Hello community, I need your help, this is a script in which a TextLabel shows the “Food”, but it does it as a leaderstat, I would like you to help me in how to put how much to cattle and never subtract it but keep adding it as “Total Food”, please I need it.
Food = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
while true do
local amount = "🌟 ".. player.leaderstats['🌟 Food'].Value
wait(0.5)
Food.Text += amount
end
If how much food the player has but if the player adds other money then that in this text is added and if you subtract the money in the leaderstats that is not subtracted from the text, because what I want to do is that it is a TOTAL FOOD STATS
Food = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local amount = player.leaderstats['🌟 Food'].Value
local newamount = amount + Food
while true do
Food.Text = "🌟 "..newamount
wait(0.5)
end
Hey! I see you’re trying to make a TOTAL FOOD Text in your TextLabel correct?
What I would do is:
I would use a .Changed connection, which would trigger everytime your Food leaderstat changes, I would also have a variable which would contain the last value your food had before the connection fired.
With this I would get the last value it had and the current value, I would check if the current value is higher than the last value, which means you GAINED food instead of losing it.
If the value is in fact higher then I would add the difference to the TextLabel (CurrentValue - LastValue) which would result in how much you gained.
Everytime it checks for the value the lastValue variable would become Food.Value again.
I would add that value into the TOTAL FOOD TextLabel, the script would be like this.
local Player = game.Players.LocalPlayer
local TextLabel = script.Parent
local FoodStat = player.leaderstats['🌟 Food']
local LastValue = Food.Value
function CheckForValue()
local Result
if FoodStat.Value >= LastValue then
Result = FoodStat.Value - LastValue
LastValue = FoodStat.Value
return Result
end
LastValue = FoodStat.Value
return 0
end
FoodStat.Changed:Connect(function()
TextLabel.Text = tonumber(TextLabel.Text) + CheckForValue()
end)
In fact this needs a bit of complicated logic, but I believe with enough thinking you can get it.
Hopefully this helps!
local Player = game.Players.LocalPlayer
local TextLabel = script.Parent
local FoodStat = Player.leaderstats['🌟 Food']
local LastValue = FoodStat.Value
function CheckForValue()
local Result
if FoodStat.Value >= LastValue then
Result = FoodStat.Value - LastValue
LastValue = FoodStat.Value
return Result
end
LastValue = FoodStat.Value
return 0
end
FoodStat.Changed:Connect(function()
TextLabel.Text = tonumber(TextLabel.Text) + CheckForValue()
end)