So I've been reading through the wiki for a few hours, trying to learn basic lua. I've come up with this, which does not work.
[lua]function getplayername(player)
if player:name(RoflWaffle) then
Msg("Hi roflwaffle")
end
end
[/lua]
I get no errors, it just doesn't do what it is supposed to. Can anyone see what I am doing wrong here?
Appreciated in advance.
Oh, and sorry for the dumb question.
Lua is case sensitive, player:name should be player:Name.
If you want to check if the players name is equal to RoflWaffle then you need to use the equality operator (==) like this:
[lua]function getplayername(player)
if player:Name() == "RoflWaffle" then
Msg("Hi roflwaffle")
end
end[/lua]
Also, don't forget to use quotes around strings.
Ah, thanks. Let me go see if that works.
[editline]11:24AM[/editline]
Hmm, didn't work.
Are you calling the function anywhere?
[QUOTE=MakeR;22040076]Are you calling the function anywhere?[/QUOTE]
That was the problem. Changed it to this
[lua]
function getplayername(player)
if player:Name() == "RoflWaffle" then
Msg("Hi roflwaffle")
end
end
concommand.Add( "test", getplayername )
[/lua]
and it worked. Thanks.
Will post here rather than making a new thread if I need any more help.
[editline]11:40AM[/editline]
Alright, ran into another problem.
[lua]
function getplayername(player)
if Player:Name() == "RoflWaffle" and Player:SteamID() == "STEAM_0:1:25793464" then
Msg("Hi roflwaffle")
else
Msg("Your not RoflWaffle")
end
end
concommand.Add( "test", getplayername )
[/lua]
I get this error:
test.lua:2: attempt to index global 'Player' (a function value)
Again it's case sensitive, change Player to player
Ah, thanks.
[editline]01:00PM[/editline]
Alright. Tried making a script so only I can spawn weapons.
[lua]
hook.Add("PlayerGiveSWEP", "AdminOnlySWEPs", function( ply, class, wep )
return player:SteamID() == "STEAM_0:1:25793464"
end)
[/lua]
And I get this error:
Hook 'AdminOnlySWEPs' Failed: weaponrestriction.lua:2: attempt to call method 'SteamID' (a nil value)
use ply instead of player
[QUOTE=Tobba;22041286]use ply instead of player[/QUOTE]
That worked, thanks.
But, I am still able to spawn it by right clicking. Is there a way to restrict this, too?
[lua]
hook.Add("PlayerSpawnSWEP", "AdminOnlySWEPs", function( ply, class, wep )
end)
[/lua]
In this case if you put "return true" in this function the weapons will spawn if you do "return false" weapons won't spawn.
So you could say
[lua]
hook.Add("PlayerSpawnSWEP", "AdminOnlySWEPs", function( ply, class, wep )
if ply:SteamID() == "Insert ID here" then
return true
else
return false
end
end)
[/lua]
[QUOTE=ColdFusion;22042195][code]
hook.Add("PlayerSpawnSWEP", "AdminOnlySWEPs", function( ply, class, wep )
end)
[/code]
In this case if you put "return true" in this function the weapons will spawn if you do "return false" weapons won't spawn.
So you could say
[lua]
hook.Add("PlayerSpawnSWEP", "AdminOnlySWEPs", function( ply, class, wep )
if ply:SteamID() == "Insert ID here" then
return true
else
return false
end
end)
[/lua][/QUOTE]
No, you don't have to do an if statement at all.
[lua]hook.Add("PlayerSpawnSWEP", "AdminOnlySWEPS", function(ply)
return ply:SteamID() == "STEAM_0:1:25793464"
end)[/lua]
[editline]03:43PM[/editline]
Unless you don't want to prevent all other PlayerSpawnSWEP hooks from running.
Alright, working on a simple anticheat. I was planning on using file.exists, but the wiki wasn't too informative on it. Can someone give me a example of how it would be used?
[lua]if file.Exists( "../lua/autorun/client/autoaim.lua" ) then
print( "We have AutoAim!" )
end[/lua]
Meh -snip-
Alright, might as well bump this rather then making a whole new thread.
Is there a way to play a sound to the client without console commands or would that be my only way? And if console commands are the only way, is there a way to stopsounds without the stopsounds concommand?(Garry blocked it)
Use [b][url=wiki.garrysmod.com/?title=surface.PlaySound]surface.PlaySound [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] to play sounds on the client.
[lua]function lockdown(player)
if player:team == TEAM_MAYOR or player:IsSuperAdmin()
then
player:ChatPrint("A lockdown has been initated by the Mayor. Please get indoors!")
surface.PlaySound("ambient/alarms/alarm_citizen_loop1.wav")
end
else
player:ChatPrint("A lockdown has been initated by the Mayor. Please get indoors!")
end
concommand.Add("lockdown", lockdown)
[/lua]
Would this work?
[lua]function lockdown(player)
if player:team == TEAM_MAYOR or player:IsSuperAdmin()
then
player:ChatPrint("A lockdown has been initated by the Mayor. Please get indoors!")
surface.PlaySound("ambient/alarms/alarm_citizen_loop1.wav")
else
player:ChatPrint("A lockdown has been initated by the Mayor. Please get indoors!")
end
end
concommand.Add("lockdown", lockdown)
[/lua]
I do believe this would work
Would there be a way to stop the sound? (garry blocked stopsounds for some dumb reason)
No. Your syntax and indentation is incorrect and this probably won't do what you want it to.
Here's how I would do it :
[lua]-- Serverside Code!
concommand.Add("lockdown", function(ply) -- I replaced player by ply so that we can use the player library if we want.
if ply:Team() == TEAM_MAYOR or ply:IsSuperAdmin() then -- It's Team() and not team. Lua is case sensitive and you need to use brackets to call a function.
SendUserMessage("LockdownInitiated") -- We're sending a message to all players.
end
end)
-- Clientside code!
usermessage.hook("LockdownInitiated", function() -- When they receive this message, this function is executed.
chat.AddText(Color(255,0,0).."WARNING :"..Color(255,255,255).."A lockdown has been initated by the Mayor. Please get indoors!") -- You can color your text!
surface.PlaySound("ambient/alarms/alarm_citizen_loop1.wav") -- This can only be used clientside.
end)[/lua]
[b][url=http://wiki.garrysmod.com/?title=G.SendUserMessage]G.SendUserMessage [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[b][url=http://wiki.garrysmod.com/?title=Chat.AddText]Chat.AddText [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[b][url=http://wiki.garrysmod.com/?title=Usermessage.Hook]Usermessage.Hook [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
If you really need to start and stop sounds at will you might want to play around with this :
[b][url=http://wiki.garrysmod.com/?title=G.CreateSound]G.CreateSound [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
Appreciate it, I'll test that out in a bit. Will post if I need any more help.
[QUOTE=Crazy Quebec;22583295][lua]-- Serverside Code!
concommand.Add("lockdown", function(ply) -- I replaced player by ply so that we can use the player library if we want.
if ply:Team() == TEAM_MAYOR or ply:IsSuperAdmin() then -- It's Team() and not team. Lua is case sensitive and you need to use brackets to call a function.
SendUserMessage("LockdownInitiated") -- We're sending a message to all players.
end
end)
-- Clientside code!
usermessage.hook("LockdownInitiated", function() -- When they receive this message, this function is executed.
chat.AddText(Color(255,0,0).."WARNING :"..Color(255,255,255).."A lockdown has been initated by the Mayor. Please get indoors!") -- You can color your text!
surface.PlaySound("ambient/alarms/alarm_citizen_loop1.wav") -- This can only be used clientside.
end)[/lua][/QUOTE]
You don't concatenate colors in chat.AddText, you pass them as separate arguments.
[lua]chat.AddText( Color( 255, 0, 0 ), "WARNING:", color_white, " A lockdown has been initiated by the mayor. Please get indoors!" )[/lua]
Oh you're right, I wasn't really thinking. :smile:
Sorry, you need to Log In to post a reply to this thread.