Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

0

I can see that the script is in StarterCharacterScripts, however, is it a LocalScript or a (Server) Script?

0

It's a Server Script

0

If it's a server script you can't use LocalPlayer

1 answer

✓ Accepted answer

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)

2 comments

0

What is "creator" supposed to do?

0

Creator is a tag when a player dies that is an objectvalue that is linked to the person that killed them

This discussion is preserved as it was. New answers, comments, and votes are closed.