LUA Error

[lua]
if SERVER then
for k, v in pairs(player.GetAll()) do
v.Money = 100
v.Salary = 45
end

function salary()
	for k, v in pairs(player.GetAll()) do
		v.Money = v.Money + v.Salary
		v:PrintMessage(HUD_PRINTTALK, "Money: "..v.Money)
	end
	timer.Simple(10, salary)
end

salary()

Weapons={}
Weapons[1]={item="weapon_crowbar", price=150}
Weapons[2]={item="weapon_stunstick", price=250}
Weapons[3]={item="weapon_pistol", price=500}

function buy_weapon(ply, value, item)
	if ply.Money >= value then
		ply.Money = ply.Money - value
		ply:Give(item)
	end
end

function chatCommand(ply, text, public)
	chatText = string.Explode(" ", text)
	if chatText[1] == "/buyweapon" then
		ply:PrintMessage(HUD_PRINTTALK, Weapons[chatText[2]].item)
		//buy_weapon(ply, 100, "weapon_crowbar")
	end
end

hook.Add( "PlayerSay", "chatCommand", chatCommand );

end
[/lua]

I’m getting this error:
[lua]

[ERROR] lua/moneysystem.lua:32: attempt to index a nil value

  1. v - lua/moneysystem.lua:32
  2. unknown - lua/includes/modules/hook.lua:82
    [/lua]

Which of these is line 32 ?

[lua]ply:PrintMessage(HUD_PRINTTALK, Weapons[chatText[2]].item)[/lua]

Make your table like this
[lua]
Weapons={
weapon_crowbar = {price = 150},
weapon_stunstick = {price = 250},
weapon_pistol = {price = 500}
}
[/lua]

Edit: And change your line 32 to this
[lua]
ply:PrintMessage(HUD_PRINTTALK, Weapons[chatText[2]].price)
[/lua]

I still get an error. Maybe it is because of that Weapons[chatText[2]] returns a string and not a number?

Edit: No, it’s not cause of this…

What’s the error?

Still the same as before.

I guess it can’t reach the “Weapons” array.

I just tried it and it works for me, try to re-paste this thing…maybe you made a mistake when changing it.

[lua]
if SERVER then
for k, v in pairs(player.GetAll()) do
v.Money = 100
v.Salary = 45
end

function salary()
	for k, v in pairs(player.GetAll()) do
		v.Money = v.Money + v.Salary
		v:PrintMessage(HUD_PRINTTALK, "Money: "..v.Money)
	end
	timer.Simple(10, salary)
end

salary()

Weapons={
	weapon_crowbar = {price = 150},
	weapon_stunstick = {price = 250},
	weapon_pistol = {price = 500}
}

function buy_weapon(ply, value, item)
	if ply.Money >= value then
		ply.Money = ply.Money - value
		ply:Give(item)
	end
end

function chatCommand(ply, text, public)
	chatText = string.Explode(" ", text)
	if chatText[1] == "/buyweapon" then
		ply:PrintMessage(HUD_PRINTTALK, Weapons[chatText[2]].price)
		//buy_weapon(ply, 100, "weapon_crowbar")
	end
end

hook.Add( "PlayerSay", "chatCommand", chatCommand );

end
[/lua]

It works until you write “/buyweapon …” in the chat, then you’ll get the error.

What are you writing in chat, just to make sure.

Oh sorry, I put in the wrong syntax…
I wrote “/buyweapon 1” but only “/buyweapon weapon_crowbar” worked.
Thank you :slight_smile: