I need help with this script

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

i need help please :frowning: -

I’m sorry I don’t understand what you are trying to achieve. You want a text label that shows how much food the player has?

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

Do you want the TextLabel to match the player’s leaderstat value?

Not that it coincides, but that the “Food” is added

How to add up the food, every time food is collected

Wait so how much food they have obtained overall? So if they eat food the text doesn’t change, but if they gain food the text does increase?

That’s what I want to do, that if you get food you increase the text

local amount = player.leaderstats[' star Food'].Value

local newamount = amount + Food

Food.Text = "star "..newamount

its dont work, bro :frowning: , What do I do?

Are there any errors in the output tab or in your script?

no error is output

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

Put amount and newamount inside of the while true do

itNeither doesnt work bro :frowning:

is this a local script or a server script?

should of asked earlier

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!

1 Like

s a localscript inside the Startergui

Food is script.Parent? What is the parent of the script I may ask? Your trying to add them both together.

its dont work bro

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)