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

How can I fix this Kill Added script? I need help please and thank you

Hello! I am working on a FFA game and when I wanted to make a kill counter, it doesn't work and I don't get what's happening.

One thing I did notice is that in the output it said that I attempted to index nil with the "leaderstats" folder.

I'd be happy to find a solution from your answers! Thanks!

Code used:

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
	wait(1)
	local leaderstats = Players.LocalPlayer.leaderstats -- This is the line that the error in the output mentioned
	local kills = leaderstats.Kills
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Died:Connect(function()
				for i, child in pairs(humanoid:GetChildren()) do
					if child:IsA('ObjectValue') and child.Value and child.Value:IsA("Player") then
						local killer = child.Value
						if killer:FindFirstChild("leaderstats") and killer.leaderstats:FindFirstChild("Kills") then
							killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
						end
					end
				end
			end)
		end
	end)
end)

1 answer

Hopefully this works, I checked it out and it looks like you were trying to reference Players.LocalPlayer which only works in LocalScripts, meaning you can't use it in a normal script. Since you already put player in the () on the Players.PlayerAdded you only had to do local leaderstats = player.leaderstats.

Hopefully it works, if it doesn't work make sure you have made it in a normal script. Good luck on your FFA game!

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
    wait(1)
    local leaderstats = player.leaderstats -- This is the line that the error in the output mentioned
    local kills = leaderstats.Kills
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
            humanoid.Died:Connect(function()
                for i, child in pairs(humanoid:GetChildren()) do
                    if child:IsA('ObjectValue') and child.Value and child.Value:IsA("Player") then
                        local killer = child.Value
                        if killer:FindFirstChild("leaderstats") and killer.leaderstats:FindFirstChild("Kills") then
                            killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
                        end
                    end
                end
            end)
        end
    end)
end)

2 comments

0

It doesn't seem to work, and as long as I changed the "ObjectValue" to "IntValue" at line 12 (because I was using an IntValue for the kill count) still doesn't seem to work and there are no errors printed into output?

0

hm. could you maby try to make the script in another way?

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