Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] Kill leaderstat script error: attempt to index local 'plr' (a nil value)?

Asked by 5 years ago
Edited 5 years ago

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!

1
GetPlayerFromCharacter(char) returns nil, meaning char is not the character. Try doing local char = hit:FindFirstAncestorOfClass("Model") Amiaa16 3227 — 5y
0
I changed it to that and now I get the error: attempt to perform arithmetic on field 'Kills' (a userdata value) DogeDark211 81 — 5y
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) User#20388 0 — 5y
0
Oh, right. I always forget .Value, Thanks! DogeDark211 81 — 5y
0
np User#20388 0 — 5y

Answer this question