Hello! I want to make smooth increasing and decreasing value of money
Now, if i get 100 coins, it will just set + 100 but how can I make increasing value like in RoyaleHigh or Adopt Me?
My script for leaserboard:
local stat = "Pearls"
local startamount = 0
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("moneysave")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = stat
Cash.Value = ds:GetAsync(player.UserId) or startamount
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Pearls.Value)
end)
My script for textlabel:
local plr = game.Players.LocalPlayer
local leaderstats = plr:WaitForChild("leaderstats")
local Pearls = leaderstats:WaitForChild("Level")
while wait() do
script.Parent.Text = ""..Pearls.Value
end
First, you should not set the data anytime the cash is changed, instead you should ONLY save the data after the player leaves.
Change the cash ds to this:
local stat = "Pearls"
local startamount = 0
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("moneysave")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = stat
Cash.Value = ds:GetAsync(player.UserId) or startamount
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Pearls.Value)
end)
You also should change ur text label to this. As a while wait() do can take up some recourses on the client
local plr = game.Players.LocalPlayer
local leaderstats = plr:WaitForChild("leaderstats")
local Pearls = leaderstats:WaitForChild("Level")
local function updateText()
script.Parent.Text = string.format("%d", Pearls.Value)
end
updateText() -- call it to update when the player first joins
Pearls.Changed:Connect(updateText)
If you’re looking for smooth changing you can do the following:
local plr = game.Players.LocalPlayer
local leaderstats = plr:WaitForChild("leaderstats")
local Pearls = leaderstats:WaitForChild("Points")
local function updateText()
local currentValue = tonumber(script.Parent.Text) or Pearls.Value
for i = currentValue, Pearls.Value, currentValue > Pearls.Value and -1 or 1 do
script.Parent.Text = string.format("%d", i)
task.wait(0.01)
end
end
updateText() -- call it to update when the player first joins
Pearls.Changed:Connect(updateText)
Although I don’t like spoon-feeding code, this is basically what you want. (this could be more optimized, but this works as you want it to)
local plr = game.Players.LocalPlayer
local leaderstats = plr:WaitForChild("leaderstats")
local Pearls = leaderstats:WaitForChild("Level")
while true do
wait()
local oldValue = tonumber(script.Parent.Text)
if oldValue < Pearls.Value then
for i = oldValue, Pearls.Value do
script.Parent.Text = i
local randomWait = math.random(oldValue,(oldValue + ((Pearls.Value - oldValue)/10)))
if randomWait == oldValue then
wait()
end
end
else
script.Parent.Text = tostring(Pearls.Value)
end
end
Here’s an edited version that should do something like that:
local plr = game.Players.LocalPlayer
local leaderstats = plr:WaitForChild("leaderstats")
local Pearls = leaderstats:WaitForChild("Points")
local function updateText()
local currentValue = tonumber(script.Parent.Text) or Pearls.Value
for i = currentValue, Pearls.Value, currentValue > Pearls.Value and -1 or 1 do
script.Parent.Text = string.format("%d", i)
task.wait(100 / Pearls.Value)
end
end
updateText() -- call it to update when the player first joins
Pearls.Changed:Connect(updateText)
it’s too slow if i need to change 1000000. I tried to use game:GetService("RunService").Stepped:Wait(.00000001) but it also very slow.
How can i change it faster like here
What you could do is change the amount in the TextLabel using for loop when the value is changed. I really don’t feel like spoon feeding at this moment but this will be an example:
local player = game.Players.LocalPlayer;
local leaderstats = player:WaitForChild("leaderstats");
local Pearls = leaderstats:WaitForChild("Level");
local OldPearlsValue = Pearls.Value;
Pearls.Changed:Connect(function(NewValue)
local step = 1;
if OldPearlsValue > NewValue then
step = -1;
else
step = 1;
end;
for i = OldPearlsValue, NewValue, step do
task.wait(0.01);
script.Parent.Text = tostring(i);
OldPearlsValue = NewValue;
end;
end);