Ad
Log in to vote
0

How to change values of leaderstats in a public script?

Asked by 8 days ago

I am having issues getting access to a players leaderstats folder, I have tried PlayerAdded but that will only run once and I am using while true do loops to update their information, not the best way of doing it and especially not now since I ran into a problem and having to work backwards from my code, I feel like I have to use a localscript but that wont update for other peoples perspectives. If you can direct me in the right direction that would be greatly appreciated <3.

1 answer

Log in to vote
0
Answered by
sO_Ov 275 Moderation Voter
8 days ago

I don't understand what is exactly your problem, but here's example of how you can access a specific folder and change values inside her

local Players = game:GetService("Players")

--// Handling Joined event and creating folder
Players.PlayerAdded:Connect(function(plr)
   --//Creating Instances
   local Folder = Instance.new("Folder")
   Folder.Name = 'leaderstats'
   Folder.Parent = plr

   local Value = Instance.new("NumberValue")
   Value.Name = 'Cash'
   Value.Parent = Folder
end)

--// Adding Cash to players
while wait(5) do
    for i,v in next, Players:GetPlayers() do -- get all players in game
      if v:FindFirstChild("leaderstats") then -- check if the folder already exists
         if v.leaderstats:FindFirstChild("Cash") then -- check if the value already exists
            v.leaderstats.Cash.Value += 10 -- get atual cash amount and increment 10
         end
      end
    end
end
Ad

Answer this question