Adding a new leaderstat

I am adding a new leaderstat to my game that will be synced up with one that has already been existing. Meaning that when you get +1 of one it also gives +10 of another. Is there a way that i can match the amount that players already have. for example if someone has 10 of the first stat when i release the update they should already have 100 of the second stat.

2 Likes

Hi! Yes you can most certainly do that!

Assuming stat1 is the current stat and stat2 is the new stat and that you have a leaderstats system in place already (with working DataStores)…

--Get Services
local Players = game:GetService("Players")

local function addNew(player)
     local leaderstats = player:FindFirstChild("leaderstats")
     if not leaderstats then return end -- Could not find player leaderstats, make sure they have a leaderstats folder

     local stat2 = leaderstats:FindFirstChild("stat2")
     local stat1 = leaderstats:FindFirstChild("stat1")
     if not stat2 then
          local stat2 = Instance.new("IntValue")
          stat2.Name = "stat2"
          stat2.Value = stat1.Value * 10 -- Most important line (whether it is 0 or 100, it will scale accordingly)
          stat2.Parent = leaderstats
     end
end

Players.PlayerAdded:Connect(addNew)

Please keep in mind that this is a sample implementation! Hope this helps!

2 Likes

In your code why do you expect these instances to be within the player if its their first time joining?

4 Likes

Hi! Its a sample implementation, so when I implement, it usually is within the player object. The whole point is to be able to locate an existing leaderstats profile for the player, if not then you know the player doesn’t have a leaderstats.

1 Like

Im saying this will not run because the leaderstats folder is getting created around the the same time because they’re both connected to the same event. Why couldn’t you just multiply the values when they’re getting created? And yes I am aware it is a sample im just pointing out stuff

1 Like

Yes I agree, which is why exactly I pointed out that it was a sample implementation, not a full scale implementation which handles the creation of leaderstats. My idea was to show the general implementation on updating values. In my implementation, I was multiplying the values and giving some context, if a player does not have specific values. This is generally how I would go about it, which has been working for me.

As for your other point, I understand the timing issue, generally I will make one call to the PlayerAdded Event, with a sequential list of function calls. This is probably not the most efficient, but it is a working implementation.

image
(An example from one of my recent projects)

I am sure there are other methods, but I wanted to share my approach!

2 Likes

Once they join check their 1 stat amount, then add it to 2 stat value (And multiply by 10)

Example:

local FirstStat = playerData.Stat1
local SecondStat = playerData.Stat2

SecondStat = (FirstStat*10)

You could also loop it to set second stat to first stat*10 if you want to
It will give same result (Not += so it doesn’t dupes money)

1 Like