How do i have 2 leader stats at once?

I would like to have 2 leader stats working at once, but it will only chose one or the other.

I am trying to have a kill leader stat and a Coin leader stat working at the same time.

someone said that its because i have 2 Instances but i am new to scripting so i do not know how to script yet.

Here is the script for the kill leader stats -
local Players = game.Players

local Template = Instance.new ‘BoolValue’
Template.Name = ‘leaderstats’

Instance.new(‘IntValue’, Template).Name = “Kills”

Players.PlayerAdded:connect(function(Player)
wait(1)
local Stats = Template:Clone()
Stats.Parent = Player

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
						local Kills = Killer.leaderstats.Kills
						Kills.Value = Kills.Value + 1
					end
					return
				end
			end
		end)
	end
end)

end)

And here is the coins leader stats-

local function onPlayerJoin(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

end

game.Players.PlayerAdded:Connect(onPlayerJoin)

Those are the scripts that i have, and most people say its because 2 Instances. But i have very little experience on how to script yet, so if someone can just say the new entire script That would be great :smiley:

and i am hoping that someone could make changes to the coin script.

I Hope everyone has a great day!

.

2 Likes

You are generating 2 leaderstats folders due to it being in 2 different scripts. You would have to put both in the same script.

Player.CharacterAdded:connect(function(Character)

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

local coins = Instance.new("IntValue")
coins.Name = "Coins"

local kills = Instance.new("IntValue")
kills.Name = "Kills"

	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
						local Kills = Killer.leaderstats.Kills
						Kills.Value = Kills.Value + 1
					end
					return
				end
			end
		end)
	end
end)
2 Likes

The issue is that you have multiple leaderstats folders. You should add all leaderstats once when the player joins like so:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
 local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"
 leaderstats.Parent = plr

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

 local coins = Instance.new("IntValue")
 coins.Name = "Coins"
 coins.Parent = leaderstats
end)
5 Likes

I also suggest reading about DataStores so you can save that data when the player joins or leaves.

1 Like

@RABDTeam I have already done that, Thank you for the suggestion.

Thank you for the reply, but this does not seem to make the kills work anymore and it does not make the coins stats show up. Do you have anymore suggestions? And Thank you for telling me the problem!

Sorry I forgot to add some things. Here is the updated version:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:connect(function(Character)

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

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

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

		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
						local Kills = leaderstats.Kills
						Kills.Value = Kills.Value + 1
						return
					end
				end
			end)
		end
	end)	
end) 

Thank you for the reply, this does seem to work , but not in the way it needs to. I am still having a problem with having 2 leader stats with the kills and coins. can you send me the edited version of both kills and coins script? Please? (i am very sorry that i do not know how to do this stuff yet, im still learning)

I just need the kills and coins script to not be creating a new leader stat.

Try the script I gave above. Should work.

i mint with your script also making the new leader stat, if that makes more since.

There is still a problem. now when i die i lose the kills. how about you tell me how to make the coin script go into the leader stats. (i mean like you tell me how to make the coin script making leader stats into the same folder as leader stats.)

You just make it in the same script. Here is the code without the kills thing:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:connect(function(Character)

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

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

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

This would be in one script. Not two.

i do not need a new leader stats folder. i already have one. the problem is putting the coins one in the same thing at the kills folder (inside the “leaderstats” folder)

Oh I see,

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:connect(function(Character)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"

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

There

Or

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

		local coins = Instance.new("IntValue")
		coins.Name = "Coins"
		coins.Parent = player:WaitForChild("leaderstats")
end)