Hi there! im wanting to make a permanent weapon system where i can type in console permwep "STEAMID" "WEAPON"
im quite new to lua and confused on how to target the player and how to save the table unique to them
this is what i came up with so far which is not good at all
[CODE]function AddWeapons( ply )
ply.UsersWeps={}
ply.UsersWeps[1]="First Entry"
table.insert( ply.UserWeps, VARIABLEFORGIVENWEAPON )
ply:SetPData( "weapons", ply.UserWeps )
local ply.GetMyWeapons = ply:GetPData( "weapons" )
ply:Give( "ply.GetMyWeapons")
end[/CODE]
please can someone help me?
[CODE]
local playerMeta = FindMetaTable("Player")
function playerMeta:GivePermaWeapons( wep )
local weps = self:GetPData("weapons")
table.insert(weps, wep)
self:SetPData("weapons", weps)
self:Give(wep)
end
ply:GivePermaWeapon("weapon_smg")
[/CODE]
Then you would use the [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerSpawn]GM/PlayerSpawn[/url] hook to give them the weapon again when they spawn.
wow thank you so much! ive needed this since shrek shutdown coderhire!
[editline]6th May 2015[/editline]
[QUOTE=_FR_Starfox64;47671560][CODE]
local playerMeta = FindMetaTable("Player")
function playerMeta:GivePermaWeapons( wep )
local weps = self:GetPData("weapons")
table.insert(weps, wep)
self:SetPData("weapons", weps)
self:Give(wep)
end
ply:GivePermaWeapon("weapon_smg")
[/CODE]
Then you would use the [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerSpawn]GM/PlayerSpawn[/url] hook to give them the weapon again when they spawn.[/QUOTE]
so if im right ply:GivePermaWeapon adds that weapon to their permanent weapons?
[editline]6th May 2015[/editline]
calling me dumb is cool but not very helpful, so would the console command be something like this
[CODE]
concommand.Add( "permawep_add", function( victim, weapon )
victim:GivePermaWeapons(weapon)
end)
[/CODE]
[QUOTE=_FR_Starfox64;47672018][IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/concommand/Add"]concommand.Add[/URL]
[IMG]http://puu.sh/hDKi9.png[/IMG][/QUOTE]
sorry but im still confused by this on what cmd, args and argStr is
The first key of the table of arguments would be the weapon (args) which is the third argument of the callback function.
Use these to see whats in a variable:
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/print]Global.print[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/PrintTable]Global.PrintTable[/url]
so would it be like this?
[CODE]
concommand.Add( "permawep_add", function(args, args2 )
print(args, args2)
end)
[/CODE]
it wouldnt just print it aswell
[IMG]http://puu.sh/hDM3s.png[/IMG] != [IMG]http://puu.sh/hDM61.jpg[/IMG]
Do the maths
like this?
[CODE]
concommand.Add( "permawep_add", function(args, argStr )
print(args, argStr)
end)
[/CODE]
but it only prints
[CODE] Player [1]N00bister permawep_add[/CODE]
[QUOTE=TroulesHere;47672257]like this?
[CODE]
concommand.Add( "permawep_add", function(args, argStr )
print(args, argStr)
end)
[/CODE]
but it only prints
[CODE] Player [1]N00bister permawep_add[/CODE][/QUOTE]
No.
[code]
concommand.Add("permawep_add", function(ply, cmd, args)
print(ply, args[1])
end)
[/code]
thanks but now i still have the problem with the code Starfox gave me
My code:
[CODE]
concommand.Add("permawep_add", function(ply, cmd, args)
ply:GivePermaWeapon(args[2])
end)
[/CODE]
it brings up this error
[CODE]
[ERROR] gamemodes/gt/gamemode/permweapons.lua:14: attempt to call method 'GivePermaWeapon' (a nil value)
1. unknown - gamemodes/gt/gamemode/permweapons.lua:14
2. unknown - lua/includes/modules/concommand.lua:54
[/CODE]
[editline]6th May 2015[/editline]
and by the way the command im using is
permawep_add N00bister weapon_smg
[editline]6th May 2015[/editline]
same with this
[CODE]
concommand.Add("permawep_add", function(ply, cmd, args)
args[1]:GivePermaWeapon(args[2])
end)
[/CODE]
[QUOTE=TroulesHere;47672314]thanks but now i still have the problem with the code Starfox gave me
My code:
[CODE]
concommand.Add("permawep_add", function(ply, cmd, args)
ply:GivePermaWeapon(args[2])
end)
[/CODE]
it brings up this error
[CODE]
[ERROR] gamemodes/gt/gamemode/permweapons.lua:14: attempt to call method 'GivePermaWeapon' (a nil value)
1. unknown - gamemodes/gt/gamemode/permweapons.lua:14
2. unknown - lua/includes/modules/concommand.lua:54
[/CODE]
[editline]6th May 2015[/editline]
and by the way the command im using is
permawep_add N00bister weapon_smg
[editline]6th May 2015[/editline]
same with this
[CODE]
concommand.Add("permawep_add", function(ply, cmd, args)
args[1]:GivePermaWeapon(args[2])
end)
[/CODE][/QUOTE]
He forgot an s, so it should be ply:GivePermaWeapon[B]s[/B]. Also, you need to convert the table to a JSON string using [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/util/TableToJSON"]util.TableToJSON[/URL] because tables do not save to PData; all it does is convert the table to a string using [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/sql/SQLStr"]sql.SQLstr[/URL] and saves it in a TEXT field. Also, you need to have a default value otherwise you will throw a Lua error if there is no PData entry. Try this:
[code]
local playerMeta = FindMetaTable('Player')
function playerMeta:GivePermaWeapons( wep )
local weps = util.JSONToTable(self:GetPData("weapons", '')) or {}
table.insert(weps, wep)
self:SetPData("weapons", util.TableToJSON(weps))
self:Give(wep)
end
ply:GivePermaWeapons("weapon_smg")
[/code]
this still isnt working, its saying that Give is a nil value?
[QUOTE=TroulesHere;47678002]this still isnt working, its saying that Give is a nil value?[/QUOTE]
Post the full error message and the entire code you are using.
my code
[CODE]
local playerMeta = FindMetaTable('Player')
function playerMeta:GivePermaWeapons( wep )
local weps = util.JSONToTable(self:GetPData("weapons", '')) or {}
table.insert(weps, wep)
self:SetPData("weapons", util.TableToJSON(weps))
self:Give(wep)
end
function GivePermWeapon(ply, cmd, args)
ply:GivePermaWeapons(args[2])
end
concommand.Add( "add_permweap", GivePermWeapon )
[/CODE]
my error
[CODE]
[ERROR] gamemodes/gt/gamemode/permweapons.lua:26: attempt to call method 'Give' (a nil value)
1. GivePermaWeapons - gamemodes/gt/gamemode/permweapons.lua:26
2. unknown - gamemodes/gt/gamemode/permweapons.lua:32
3. unknown - lua/includes/modules/concommand.lua:54
[/CODE]
[editline]7th May 2015[/editline]
also i know i spelt wep wrong
What color the error is? Is it by any chance orange?
in console its yellow and on my screen (top left) its white
You are trying to give the weapon to a player from the CLIENT. What you should be doing is giving the player the weapon from the SERVER. To ensure this, make sure to:
1) Include the file permweapons.lua from init.lua
2) Wrap all of that code in "if ( SERVER ) then /*your code here*/ end"
3) Restart the game & server and try again.
ok thanks il try those
[editline]7th May 2015[/editline]
that works but if i type in console add_permwep weapon_smg it says this
[CODE]
Attempted to create unknown entity type weapon_smg!
NULL Ent in GiveNamedItem!
[/CODE]
my code
[CODE]
function GivePermWeapon(ply, cmd, args)
ply:GivePermaWeapons(args[1])
end
concommand.Add( "add_permwep", GivePermWeapon )
[/CODE]
[QUOTE=TroulesHere;47678265]ok thanks il try those
[editline]7th May 2015[/editline]
that works but if i type in console add_permwep weapon_smg it says this
[CODE]
Attempted to create unknown entity type weapon_smg!
NULL Ent in GiveNamedItem!
[/CODE]
my code
[CODE]
function GivePermWeapon(ply, cmd, args)
ply:GivePermaWeapons(args[1])
end
concommand.Add( "add_permwep", GivePermWeapon )
[/CODE][/QUOTE]
It's weapon_smg1, not weapon_smg
thanks but now im trying to get it to give them their weapons upon spawn so would the code be
[CODE]
function GM:PlayerSpawn( ply )
self:Give(weps)
end
[/CODE]
Why not, uh, try it?
[lua]
function playerMeta:GetPermaWeapons( )
return util.JSONToTable(self:GetPData("weapons", '')) or {}
end
function GM:PlayerSpawn( ply )
for _,weapon in pairs( ply:GetPermaWeapons() ) do
ply:Give(weapon)
end
end
[/lua]
Something like this
unfortunatly that didnt work...
[CODE]
[ERROR] gamemodes/gt/gamemode/permweapons.lua:27: attempt to call a table value
1. unknown - gamemodes/gt/gamemode/permweapons.lua:27
2. Spawn - [C]:-1
3. unknown - gamemodes/base/gamemode/player.lua:115
[/CODE]
[QUOTE=TroulesHere;47678748]unfortunatly that didnt work...
[CODE]
[ERROR] gamemodes/gt/gamemode/permweapons.lua:27: attempt to call a table value
1. unknown - gamemodes/gt/gamemode/permweapons.lua:27
2. Spawn - [C]:-1
3. unknown - gamemodes/base/gamemode/player.lua:115
[/CODE][/QUOTE]
It was pseudo code, but I've fixed it in my previous post.
Sorry, you need to Log In to post a reply to this thread.