I’m trying to add a mechanic to my game where it tells the player the total about of sugar (games money)they have had all-time but to do that I would have to go into all of the parts of the game (over 500) and change the script to add more to that is there a way I can just update the players total sugar and current sugar but when they rank up the total stays at what it is and the about they currently have resets to zero?
Code for leaderstats
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new(“Model”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Sugar"
money.Value = 0
money.Parent = leaderstats
local money = Instance.new("IntValue")
money.Name = "Rank"
money.Value = 0
money.Parent = leaderstats
local money = Instance.new("IntValue")
money.Name = "Pop Points"
money.Value = 0
money.Parent = leaderstats
local money = Instance.new("IntValue")
money.Name = "Total Sugar"
money.Value = 0
money.Parent = leaderstats
end)
Save data
local DataStoreService = game:GetService(“DataStoreService”)
local playerData = DataStoreService:GetDataStore(“PlayerData”)
local function onPlayerJoin(player) – Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue") --Sets up value for leaderstats
money.Name = "Sugar"
money.Parent = leaderstats
local exp = Instance.new("IntValue") --Sets up value for leaderstats
exp.Name = "Rank"
exp.Parent = leaderstats
local pop = Instance.new("IntValue") --Sets up value for leaderstats
pop.Name = "Pop Points"
pop.Parent = leaderstats
local s = Instance.new("IntValue") --Sets up value for leaderstats
s.Name = "Total Sugar"
s.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
money.Value = data['Sugar']
exp.Value = data['Rank']
pop.Value = data['Pop Points']
s.Value = data['Total Sugar']
else
-- Datastore is working, but no current data for this player
money.Value = 0
exp.Value = 0
pop.Value = 0
s.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
–
One Candy (Get sugar)
waittime = 3 – Time Between each hit
amnt = 5 --how much you get for it
function onTouched(part)
local h = part.Parent:findFirstChild(“Humanoid”)
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild(“leaderstats”)
if (stats~=nil) then
local score = stats:findFirstChild(“Sugar”)
if (score~=nil) then
score.Value = score.Value + amnt
end
end
end
script.Parent.Transparency = 1
script.Disabled = true
wait(waittime)
script.Parent.Transparency = 0
script.Disabled = false
end
end
script.Parent.Touched:connect(onTouched)