• Lua Help needed C:
    3 replies, posted
So this command is trying to say when the player equips a physgun to immediately take it away [CODE]hook.Add( "PlayerSay", "Hook1", function( ply, text, isteam ) text = string.lower( text ) if (string.sub( text, 0, 6 ) == "!ephys" ) and ( ply:IsAdmin() or ply:IsSuperAdmin() ) then text = string.Explode( "", text ) local victim = FindPlayers( text[2] ) if ( IsValid( victim ) ) then function GM:WeaponEquip( "weapon_physgun" ) victim:StripWeapon( "weapon_physgun" ) end end end return "" end )[/CODE] So this is my code. It's a command which targets a player as you can see. It's Giving me Errors and to be honest I'm not surprised. This is the part giving me errors: [CODE]function GM:WeaponEquip( "weapon_physgun" ) victim:StripWeapon( "weapon_physgun" ) end[/CODE] that piece was mostly guess work this part is the bit I need help on so it would be great if you could: 1) Tell me what I did wrong and how to fix it or 2)Tell me a different/better way to do it Thanks a lot sorry for my extreme noobishness please forgive me and... HALP xD
[B]ALWAYS[/B] use [URL="http://wiki.garrysmod.com/page/hook/Add"]hooks[/URL] not GM functions unless your coding the gamemode. It's not working because the owner of the weapon is unknown in that hook. To do this you will need to use [url=http://wiki.garrysmod.com/page/GM/PlayerCanPickupWeapon]PlayerCanPickupWeapon[/url], here is a quick example [lua] hook.Add( "PlayerCanPickupWeapon", "ExamplePickup", function( ply, wep ) if wep:GetClass() == "weapon_physgun" then return false end end ) [/lua]
[QUOTE=ZombieWizzard;47113714][B]ALWAYS[/B] use [URL="http://wiki.garrysmod.com/page/hook/Add"]hooks[/URL] not GM functions unless your coding the gamemode. It's not working because the owner of the weapon is unknown in that hook. To do this you will need to use [url=http://wiki.garrysmod.com/page/GM/PlayerCanPickupWeapon]PlayerCanPickupWeapon[/url], here is a quick example [lua] hook.Add( "PlayerCanPickupWeapon", "ExamplePickup", function( ply, wep ) if wep:GetClass() == "weapon_physgun" then return false end end ) [/lua][/QUOTE] Thanks a lot but its not about picking up the weapon. I want then to just not be able to select it.
You are very close to your goal. Here's what you did wrong: you're trying to define a function inside a function! That's bad. You also seem to be misunderstanding the concept of hooks. I'm afraid I'm not good at explaining so I can't help with that... There are two things I see wrong in your code: 1. FindPlayers - I don't see this function documented on the wiki, where did you find it and are you sure that it works? (I could be wrong) 2. Trying to define GM:WeaponEquip and put victim:StripWeapon inside it. Drop the whole function thing, you could just make it like this: [CODE]hook.Add( "PlayerSay", "Hook1", function( ply, text, isteam ) text = string.lower( text ) if (string.sub( text, 0, 6 ) == "!ephys" ) and ( ply:IsAdmin() or ply:IsSuperAdmin() ) then text = string.Explode( "", text ) local victim = FindPlayers( text[2] ) if ( IsValid( victim ) ) then victim:StripWeapon( "weapon_physgun" ) end return "" end )[/CODE]
Sorry, you need to Log In to post a reply to this thread.