Increasing and decreasing value

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 
1 Like

Could you give a video of what you mean by smooth increasing? (I’m not in my PC right now so I can’t in those games)

Also, instead of while loops check when the value of a property is changed.

Pearls.Changed:Connect(function(newval)
	script.Parent.Text = newval
end)

I’m on mobile, so formatting might not be correct.

You could try something to do with a for loop.

for i = (current money value), (target money value), (increment) do
task.wait(delay)
(TextLabel?).Text = i
end

2022-05-01 20-19-43

Now, you’re doing many things wrong.

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)

@TheLight_0fficial

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

Can I make it slower if value is small and faster if value is big?

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
2022-05-01 21-32-38

You could use TweenService. eg:

local coins = player.leaderstats.coins

-- Tween

local myProperties = {}
myProperties.Value = 10

local myTweenInfo = TweenInfo.new(
2,
Enum.EasingStyle.Quad
)

local myTween = game:GetService("TweenService"):Create(coins,myTweenInfo,myProperties)
myTween:Play

or if you want it to change speed depending on coins change then

local coins = player.leaderstats.coins
locan coinChange = 10

-- Tween

local myProperties = {}
myProperties.Value = 10

local myTweenInfo = TweenInfo.new(
math.clamp(2,5,coinChange / 100),
Enum.EasingStyle.Quad
)

local myTween = game:GetService("TweenService"):Create(coins,myTweenInfo,myProperties)
myTween:Play

Will leaderboard change smoothly or gui?

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);

leaderboard, but you can change the gui with it, change the text to the leaderboard