Troubles with leaderstats

I just tried the script, and it worked, except for one thing. It doesn’t show the other leaderstat that I already had in my game! Why?

When I remove the script from my game, the other leaderstat come back normaly.

Because it’s creating 2 leaderstats folders, which won’t work with the player list. Add to your script the value.

1 Like

How do I do that?

Anyways thank you very much for your help.

Could you show me the current script? I can look and see if there are any mistakes here assuming you have no errors related to the script. Also, check the hierarchy (Explorer) of Players. See if there are any incorrect positions of leaderstat related things. Doing that might solve the issue yourself.

1 Like

This script?

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local Leaderstats = Instance.new("Folder") -- create the leaderstats folder
	Leaderstats.Name = "leaderstats" -- MUST be lowercase
	
	local Cash = Instance.new("StringValue")
	Cash.Name = "Cash" -- change name here
	Cash.Value = "CashValue" -- a string
	Cash.Parent = Leaderstats -- make sure the value is in the folder
	
	Leaderstats.Parent = plr -- parent it to the player so it can be visible
end)

I thought you had 2 leaderstats right? I meant the one that you said is not working as I don’t see any problems with that one.

I’m not on my computer right now so I can’t send you the script but when I will be on I will send you.

Very sorry for the late answer, I wasn’t able to use my computer before.

Here is the entire script, it’s a time played saving leaderstat (it comes from a free model):

--[[
         script made by Rodemallgames
    Put this script in ServerScriptService or it won't work

--]]


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

	local timeingame = Instance.new("IntValue")
	timeingame.Name = "Time"
	timeingame.Parent = leaderstats
	while true do
		wait(1)
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 1
	end
end)

-----------------------------------------------| Save Stats |------------------------------------------------------------


local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


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

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

	local playerUserId = "Player"..player.UserId

	local data 

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId) 
	end)


	if success then
		Time.Value = data  
	end

	while wait(1) do
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 0 --Don't change it or it will make it go up twice
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player"..player.UserId

	local data = player.leaderstats.Time.Value

	myDataStore:SetAsync(playerUserId, data)   

end)

Oh, I see. You have it creating leaderstats and Time twice. Removing the duplicate instancing will fix it. I believe all you should have to remove is the first PlayerAdded connection. The bottom one where it also saves will work fine.

1 Like

Sorry to disturb you again, but how to do that? :confused:

Sorry for the slow response, I don’t check DevFourm too much.
In your script, you have game.Players.PlayerAdded connected at the top, and then a few lines below you have it again.

If you look inside the two functions and compare them, you’ll see that they both do the same thing at first, but the second one also loads previously existing data.

Because of this, you should delete the top connection of PlayerAdded, as

  1. Having two connections to PlayerAdded in the same script is not good as you can put it all into one connection
  2. It’s creating leaderstats and Time, but then the one below ALSO is creating leaderstats and Time. Because of this, there are two instances of these, which breaks the leaderstats.
  3. The top one does not load previous data, which the bottom connection does.

Hope this explains everything. :+1:

1 Like

So I just need to remove game.Players.PlayerAdded in one of the two scripts?

Thank you very much for helping me.

I’m assuming this is the only script creating leaderstat related things.
Yes, all you have to do is delete the first game.Players.PlayerAdded function at the top and keep the bottom one.

1 Like

Sorry for the late response.

So I removed the game.Players.PlayerAdded functiona at the top so it is from this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local Leaderstats = Instance.new("Folder") -- create the leaderstats folder
	Leaderstats.Name = "leaderstats" -- MUST be lowercase
	
	local Cash = Instance.new("StringValue")
	Cash.Name = "Cash" -- change name here
	Cash.Value = "CashValue" -- a string
	Cash.Parent = Leaderstats -- make sure the value is in the folder
	
	Leaderstats.Parent = plr -- parent it to the player so it can be visible
end)

to this:

Players.PlayerAdded:Connect(function(plr)
	local Leaderstats = Instance.new("Folder") -- create the leaderstats folder
	Leaderstats.Name = "leaderstats" -- MUST be lowercase
	
	local Cash = Instance.new("StringValue")
	Cash.Name = "Cash" -- change name here
	Cash.Value = "CashValue" -- a string
	Cash.Parent = Leaderstats -- make sure the value is in the folder
	
	Leaderstats.Parent = plr -- parent it to the player so it can be visible
end)

But when I test nothing change…

This is an entirely different script from the one I was assisting with (the one that saves time), and this one may have its own issues.
From the looks of it, this script seems to be okay.
Again, remember you should only be creating leaderstats once.

1 Like

I’m lost, can you show me how the script should look?

Sorry to ask for all this help.

Have a great day.

Heres how I do it, I will also explain as I script.

game.Players.PlayerAdded:Connect(function(plr) -- Whenever the player joins..
       local stats = Instance.new("Folder", plr) -- Will make a new folder and insert it into the player
       stats.Name = "leaderstats" -- Will name the folder stats.

       local ValueName = Instance.new("IntValue", stats) -- Will insert a intvalue into stats.
       ValueName.Name = "ValueName" -- Will name the value
       ValueName.Value = 0 -- How much the player starts off with.
end)

hope this helped!

1 Like

the folder name must be “leaderstats” if you want it to show as CoreGUI. Just thought I’d add that. You can create other folders that are not called leaderstats to not show them as CoreGUI, which is often useful for saving “behind the scenes” data.

1 Like

Thank you! I will edit this!

2 Likes

Is this working with the script I previously shown?