Hello, I am trying to make a "Kills" leaderstat where someone kills another player then they get + 1 on their Kills value. Whenever I test the script it gives me the error:

attempt to index local 'plr' (a nil value)

Here is my code, using a server script in the players character.

humanoid = script.Parent:FindFirstChild("Humanoid") --Gets the local players humanoid
Torso = script.Parent:FindFirstChild("Torso") --Gets the local players torso

Torso.Touched:connect(function(hit)
	--If statements to check if hit.Parent is the sword: "ATK" and the players health is 0
	if not hit.Parent:FindFirstChild("ATK") then return end
	if not humanoid.Health == 0 then return end

	local char = hit.Parent.Parent --Gets the swords character
	local plr = game.Players:GetPlayerFromCharacter(char) --Gets the player with the sword
	plr.leaderstats.Kills = plr.leaderstats.Kills + 1 --Adds 1 to the value, this is where I get the error
end)

I'm not sure why I am getting this error and if you could tell me that would be great thanks!

5 comments

1

GetPlayerFromCharacter(char) returns nil, meaning char is not the character. Try doing local char = hit:FindFirstAncestorOfClass("Model")

0

I changed it to that and now I get the error: attempt to perform arithmetic on field 'Kills' (a userdata value)

1

See, kills is an object, not a value (as you may think) so your trying to do maths on an object, wich won't work, to solve this issue you have to know that the 'value' object contains a property named 'Value' wich holds the value, so you can fix it by simply puttin .Value behind .Kills (line 11, 2 times)

0

Oh, right. I always forget .Value, Thanks!

0

np