Broken leaderstats

Hello developers, I made a leaderstats but it doesn’t work the way I want it.
Here is the leaderstats script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats

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

end)

I and I have 2 other scripts which gives you coins/kills for killing a player
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = Player

local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats


Player.CharacterAdded:Connect(function(Character)
	Character.Humanoid.Died:Connect(function(Died)
		local creator = Character.Humanoid:FindFirstChild("creator")
		local leaderstats = creator.Value:FindFirstChild("leaderstats")
		if creator ~= nil and creator.Value ~= nil then
			leaderstats.Coins.Value = leaderstats.Coins.Value + 15
		end
	end)
end)

end)

The other one is the same except the stats are for kills

When I play the game it only shows one of the stats on the leaderstats for example:
Broken leaderstats
Leaderstats

Okay so the issue is

You make the leaderstats folder again in the other two scripts, remove that and it should work

1 Like

Why is it in your first script you create coins with as IntValue, but then recreate it with NumberValue? This is redundant and will make it painful to select it.

I would try changing kills to a NumberValue instead.

1 Like

I only watch tutorials on how to make these scripts so I’m not actually a scripter but how could I change this to work?

All you have to do is as I mention, just make your scripts (not the leaderstats one) this:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
	    Character.Humanoid.Died:Connect(function(Died)
		    local creator = Character.Humanoid:FindFirstChild("creator")
		    local leaderstats = creator.Value:FindFirstChild("leaderstats")
		    if creator ~= nil and creator.Value ~= nil then
			    leaderstats.Coins.Value = leaderstats.Coins.Value + 15
		    end
	    end)
    end)
end)

And the second script:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
	    Character.Humanoid.Died:Connect(function(Died)
		    local creator = Character.Humanoid:FindFirstChild("creator")
		    local leaderstats = creator.Value:FindFirstChild("leaderstats")
		    if creator ~= nil and creator.Value ~= nil then
			    leaderstats.Kills.Value = leaderstats.Kills.Value + 15
		    end
	    end)
    end)
end)
1 Like

It seems to work I’m gonna run a few tests thank you for your help!

1 Like

It works but then again I found another problem, I don’t get any coins/kills when I kill a player.

Are you assigning a creator object value when the player dies to a certain weapon.

Basically for example any tool you can kill a player with should give you coins/kills

Instead of 2 separate scripts , you could simply union them into 1 script -

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
	    Character.Humanoid.Died:Connect(function(Died)
		    local creator = Character.Humanoid:FindFirstChild("creator")
		    local leaderstats = creator.Value:FindFirstChild("leaderstats")
		    if creator ~= nil and creator.Value ~= nil then
			    leaderstats.Coins.Value += 15
                leaderstats.Kills.Value += 15
		    end
	    end)
    end)
end)

Good idea. I don’t get why you used += instead of just a normal + though.

coins.Value += 10

will do the same thing as

coins.Value = coins.Value + 10

Also good idea @Valkyrop
Don’t know why I didn’t think of that lol

2 Likes

When you use += or -= it will add/subtract from the provided value, the amount after the =.

It can save you time.

1 Like