hook.add doesn't work!

Hello,
I wrote a simple script, that is supposed to show people the server rules when they spawn.
The problem is, that it doesnt show up… the console command can be entered but it doesn’t do anything! i put the file in the lua/autorun folder of my server.

Script:
[lua]function init()
local DermaPanel = vgui.Create(“DFrame”)
DermaPanel:SetSize( 336, 215 )
DermaPanel:SetTitle(“Welcome to Hellspawn’s Fun Server!”)
DermaPanel:SetVisible(true)
DermaPanel:SetDraggable(false)
DermaPanel:SetBackgroundBlur(true)
DermaPanel:ShowCloseButton(false)
DermaPanel:MakePopup()
DermaPanel:Center()

local DermaLabel = vgui.Create(“DLabel”)
DermaLabel:SetText( [[
THE RULES

  1. Dont spam or build Contraptions that lag the Server.

  2. Play nice, dont touch other Players Stuff without asking them.

  3. Respect the Admins and they will respect you.

  4. No random Deathmatch!!!

  5. Type Accepted in the box below and click the OK button.
    ]] )
    DermaLabel:SetParent(DermaPanel)
    DermaLabel:SizeToContents()
    DermaLabel:SetPos(10, 25)

local DermaLabel1 = vgui.Create(“DLabel”)
DermaLabel1:SetText(“Read the rules first!”)
DermaLabel1:SetParent(DermaPanel)
DermaLabel1:SizeToContents()
DermaLabel1:SetPos(230,185)
DermaLabel1:SetColor(Color(255,0,0))
DermaLabel1:SetVisible(false)

local DermaText = vgui.Create(“TextEntry”)
DermaText:SetParent(DermaPanel)
DermaText:SetSize(70,25)
DermaText:SetPos(98,180)

local DermaButton = vgui.Create(“DButton”)
DermaButton:SetParent(DermaPanel)
DermaButton:SetSize(50,25)
DermaButton:SetPos(173,180)
DermaButton:SetText(“OK”)
DermaButton.DoClick = function()
if DermaText:GetValue() == (“Accepted”) then
DermaPanel:Close()
else DermaLabel1:SetVisible(true)
end
end
end

hook.Add(“init”,“ShowMenu”,init)
concommand.Add(“rules”, init)[/lua]

First of all, your Hook will never work because hooks are case sensitive and your name is incorrect. It is not “init”, it is “Initialize”.

[del]Second, both your concommand and your hook will never be set. This is because you create them inside the “init” function: that is, until init() is run you don’t actually create the concommand or the hook. You need to make the concommand outside of the function, in an autorun file or something.[/del]

Ohhh, ok never mind. You ought to fix your indentation.

You can’t just create hooks, there is a set list of them and they must match.

[editline]15th January 2011[/editline]

Also Entoros, He is ending the function before he creates them, so the concommand would work just not the hook I’m pretty sure.

What? http://wiki.garrysmod.com/?title=Hook

Oohh, didn’t see that, darn bad formatting. Thanks c-unit. But yeah, my guess is the file isn’t being run.

Mike&Ike: c-unit means you can’t just do hook.Add(“LOLHook”,“fjkls”,myFunc) and have it work.

Exactly, like some people post with hook.Add(“bunnyhop”, “asd”, function) and are dumbfounded at why nothing happens when they run the bunnyhop command in the console

So i must rename the hook or function? I’m a little confused…

Change function to something like GameInit and #3 argument of the hook also to GameInit

I tried that. It still doesnt work :frowning:

Dude it’s because init hook is wrong, it’s Initialize.

Sorry about that i misunderstood. I would use the player initial spawn since that way it will run once they are in game. http://wiki.garrysmod.com/?title=Gamemode.PlayerInitialSpawn this might also help http://wiki.garrysmod.com/?title=Gamemode_Hooks

Before we get too caught up in hooks, let’s take a step back: where is this file located?

lua/autorun

He said before.

In which case you’ll get lots of errors as the vgui library isn’t available serverside, nor will the file be sent to the client.

Try something like this at the top:

[lua]
include( “thisfile.lua” )

if SERVER then – only run this next bit on the server
AddCSLuaFile(“thisfile.lua”) – make sure file is sent to client
hook.Add(“PlayerInitialSpawn”, “your hook name”, function( ply ) – Create a hook thats called when the player spawns, this isn’t available clientside so we’ll have to do it on the server and tell the client from there
SendUserMessage( “Init”, ply ) – Send a usermessage to the player
end)
return – stop anything below this being ran serverside
end

– put all your other code here
– then instead of using hook.Add we want to hook to the usermessage we just sent from the server
usermessage.Hook( “Init”, init )
[/lua]

Too much :downs: in this thread.

[editline]16th January 2011[/editline]

[lua]function ShowRules()
local DermaPanel = vgui.Create(“DFrame”)
DermaPanel:SetSize( 336, 215 )
DermaPanel:SetTitle(“Welcome to Hellspawn’s Fun Server!”)
DermaPanel:SetVisible(true)
DermaPanel:SetDraggable(false)
DermaPanel:SetBackgroundBlur(true)
DermaPanel:ShowCloseButton(false)
DermaPanel:MakePopup()
DermaPanel:Center()

local DermaLabel = vgui.Create("DLabel")
DermaLabel:SetText( [[
THE RULES
 
1. Dont spam or build Contraptions that lag the Server.
 
2. Play nice, dont touch other Players Stuff without asking them.
 
3. Respect the Admins and they will respect you.
 
4. No random Deathmatch!!!
					 
5. Type Accepted in the box below and click the OK button.
]] )
DermaLabel:SetParent(DermaPanel)
DermaLabel:SizeToContents()
DermaLabel:SetPos(10, 25)
 
local DermaLabel1 = vgui.Create("DLabel")
DermaLabel1:SetText("Read the rules first!")
DermaLabel1:SetParent(DermaPanel)
DermaLabel1:SizeToContents()
DermaLabel1:SetPos(230,185)
DermaLabel1:SetColor(Color(255,0,0))
DermaLabel1:SetVisible(false)
 
local DermaText = vgui.Create("TextEntry")
DermaText:SetParent(DermaPanel)
DermaText:SetSize(70,25)
DermaText:SetPos(98,180)
 
local DermaButton = vgui.Create("DButton")
DermaButton:SetParent(DermaPanel)
DermaButton:SetSize(50,25)
DermaButton:SetPos(173,180)
DermaButton:SetText("OK")
DermaButton.DoClick = function()
	if DermaText:GetValue() == ("Accepted") then
		DermaPanel:Close()
	else 
		DermaLabel1:SetVisible(true)
	end
end

end

concommand.Add(“rules”, ShowRules)
– hook.Add(HOOK_TYPE , HOOK_UNIQUEIDENTIFIER, function
hook.Add(“InitPostEntity” , “You’re all retarded and giving out incorrect advice” , ShowRules)
[/lua]

it still doesn’t work… I believe it has something to do with the directory. i have uploaded it in an addon. maybe that is the problem?

Where is it in the addons file structure?

You may be missing an info.txt

Have you called AddCSLuaFile on the path?

Eg: addons/Some_Rules/lua/autorun/client/rules.lua

AddCSLuaFile(“autorun/client/rules.lua”)

Maybe my earlier post wasn’t so ‘dumb’, as you rated it, eh?

Your post is still dumb. You’re networking from the server to tell the client that they spawned when you don’t even need to.

It STILL doesnt work D: i tried EVERYTHING!