Can anyone try and to explain me what i did wrong?

So im making a tycoon game, and i want it to have like a money collector. and the text just wont appear on the surfacegui

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)

	if player then
		local leaderstats = player:FindFirstChild("leaderstats")

		local ComV = script.Parent.CashValue
		leaderstats.points.Value = ComV.Value + leaderstats.points.Value
		local number = script.Parent.Parent.Part1.SurfaceGui.TextLabel.Text
		
		ComV.Value = 0
		
		
		while true do
			
			wait()
			number = ComV.Value.."$"
		end

	end
end)
1 Like

Not sure if this would do the trick but try placing the while true do outside the function, and make a new numberVariable at the top, you could then wright in the loop:

wait()
if numberVar then
number = ComV.Value … "$
end

(you have to define the new var when you touch the block)

1 Like

i dont think that i understood your reply, i did what you said but i dont i typed everything correctly.

1 Like

i would rather use the above, than using a while true do loop
(very expensive in terms of memory, but depends on what you are doing)

i dont think you understood what im trying to fix here… im trying to change it not to detect if it did change

number is only a reference to the string value of the text at that moment in time
use TextLabel.Text = to set the property

2 Likes

I made a script similar of what you did, but using the player health

whenever i click a button, it will update player.Health

and this is where Changed:function comes in

when Health change, ( money, or anything ) it will instantly update your value to what
you want,

example:

--Server Script
game.workspace.MoneyGiver.ClickDetector.MouseClick:Connect(function(player) 
	
	player.Money.Value = player.Money.Value + 1 -- Example

--each time you click on a part, you receive money

	print"+1 Money"

    end
end)

Now let’s check TextLabel:


--LOCAL SCRIPT
local plr = game.Players.LocalPlayer
local PlayerGui = plr:WaitForChild("PlayerGui") -- When dealing with GUI'S (Only Local)

local Money = plr:WaitForChild("Money")

Money:GetPropertyChangedSignal("Value"):Connect(function() -- Detect when Money Change

PlayerGui:WaitForChild("YourGui").TextLabel.Text = Money.Value 
-- will change every single time you get Money, the Text will update

end)

This isn’t related to your question, I just have to nitpick a little.

That while true do loop?
Each time you step on the collector, a new loop is created to update the text. It remains even when you step on it again.
Eventually, there are so many loops doing useless busy work that the server slows down to a crawl or crashes.

Here’s your script with the fix in the solution and with the loop moved out of the connection:

local Players = game:GetService("Players")
local ComV = script.Parent.CashValue
local number = script.Parent.Parent.Part1.SurfaceGui.TextLabel

script.Parent.Touched:Connect(function(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	
	if player then
		local leaderstats = player.leaderstats
		leaderstats.points.Value = leaderstats.points.Value + ComV.Value
		ComV.Value = 0
		number.Text = ComV.Value .. "$" -- update instantly
	end
end)

while true do
	number.Text = ComV.Value .. "$"
	wait()
end

The changes:

  • The solution by SavageBoyBlack
  • Moved that while loop below the connection. (If it were above it, then the connection would never be made because the infinite loop is before it.)
  • Moved the local variables to above the connection. (If they stayed in the connection, then they would only mean something inside the connection and nothing outside it (e.g. in the loop).)
  • Removed FindFirstChild because if the player doesn’t have leaderstats, then you get an error on the very next line anyway and that doesn’t permanently break the button, so why bother
  • leaderstats.points.Value = leaderstats.points.Value + ComV.Value, because that’s a clearer ordering imo
  • Added spacing to ComV.Value .. "$" just because it bothers me

Oh yea, i didnt notice it when i posted this, but i figured it out later (the while true loop) but thank you anyways!