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

my script should change a players hat depending on how many kills they have, why it doesn't work?

Asked by
ht518 4
3 years ago
Edited 3 years ago

hello, I made a script that's supposed to change the players hat depending on how many kills they have, but it doesn't work and in the output it says Workspace.ht518.Script:1: attempt to index nil with 'leaderstats'

if game.Players.LocalPlayer.leaderstats.Kills.Value == 10 

then

    script.Parent.hat:Destroy()
    game.ServerStorage.hat1:Clone()
    game.ServerStorage.hat1.Parent = script.Parent

end

can anybody help me with this, it is a (Server) Script in StarterCharacterScripts

0
I can see that the script is in StarterCharacterScripts, however, is it a LocalScript or a (Server) Script? appxritixn 2235 — 3y
0
It's a Server Script ht518 4 — 3y
0
If it's a server script you can't use LocalPlayer MattVSNNL 620 — 3y

1 answer

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago

Try this

Make a Server script in ServerScriptService and use this code

game:GetService("Players").PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Value = 0
    kills.Parent = leaderstats

    player.CharacterAdded:Connect(function(char)

        char:WaitForChild("Humanoid").Died:Connect(function()
            local creator = char:WaitForChild("Humanoid"):FindFirstChild("creator")

            if creator and creator.ClassName == "ObjectValue" then
                local murderer = creator.Value

                murderer:WaitForChild("leaderstats"):WaitForChild("Kills").Value += 1

            end
        end)

        if kills.Value >= 10 then
            char:FindFirstChild("hat"):Destroy()

            local cloneHat = game:GetService("ServerStorage"):FindFirstChild("hat1"):Clone()
            cloneHat = script.Parent
        end
    end)

end)
0
What is "creator" supposed to do? AwesomeGamer2223 105 — 3y
0
Creator is a tag when a player dies that is an objectvalue that is linked to the person that killed them MattVSNNL 620 — 3y
Ad

Answer this question