Why is my change detection script not working?

Hello, Im trying to make it so whenever the value off the ball changes, the text over the ball will set itself to the value of the ball.

Its not working, and its not erroring though.

The Script:

local Text = script.Parent.Text

local Cash = script.Parent:WaitForChild("Cash").Value

while true do
	task.wait()
	
	script.Parent:GetPropertyChangedSignal(Cash):Connect(TextChange)

	function TextChange()
		Text = tostring(Cash)
	end
end

You shouldn’t put that property changed connection inside a while loop, i suggest you put it out. And there is not such property named “cash”, are you looking for a change in the cash’s value?

Yes I am, also why should I not put it in a while true loop, wouldn’t it only run once like an if statement?

No, an if statement will run only once in the entire function/script, but an event will run the function everytime a change is detected. So you only need to connect the event once and your good

1 Like

I tried to get the value of Cash ( a Instance within the NumberValue Class ) and it didn’t seem to work.

local Text = script.Parent.Text

local Cash = script.Parent:WaitForChild("Cash")

script.Parent:GetPropertyChangedSignal(Cash.Value):Connect(TextChange)

function TextChange()
	Text = tostring(Cash)
end

Their were no errors

2 problems. First, as @TenBlocke mentioned, it’s inside of a loop, which it shouldn’t be and second, you need to create the function before connecting it to an event.

local Text = script.Parent.Text

local Cash = script.Parent:WaitForChild("Cash")

function TextChange()
	Text = tostring(Cash)
end

script.Parent:GetPropertyChangedSignal(Cash.Value):Connect(TextChange)

Why before instead of after?

What change does it make?

LUA runs much differently from Java, Python, or any other coding language that allows you to call functions before they are made. In LUA, if you call a function before it is created, it will lead to an error.

This didn’t seem to work


Ah my bad, I was focusing on the main problems. You need to change “Cash.Value” to just “Value,” you cannot reference the Text itself to change it, and you were adding the GetPropertyChangedSignal to the wrong Instance.

local Label = script.Parent

local Cash = script.Parent:WaitForChild("Cash")

function TextChange()
	Label.Text = tostring(Cash.Value)
end

Cash:GetPropertyChangedSignal("Value"):Connect(TextChange)
1 Like
local Text = script.Parent.Text

local Cash = script.Parent:WaitForChild("Cash").Value --- try with out the value

while true do
	task.wait()
	
	script.Parent:GetPropertyChangedSignal(Cash):Connect(TextChange)--- try text instead of cash, is cash a  property of a Text thing?

	function TextChange()
		Text = tostring(Cash)
	end
end

It didn’t seem to work


Try to recopy it, i had made some changes after you saw it.

Ok so are you sure that the value is getting changed?

First i will ask you to do this and tell me what do you see in output


local Label = script.Parent

local Cash = script.Parent:WaitForChild("Cash")

function TextChange()
	Label.Text = tostring(Cash.Value)
        print("Function Executed!")
end

Cash:GetPropertyChangedSignal("Value"):Connect(TextChange)

print("Finished Execution!")

I’ve got completely nothing inside of the output

See that’s what I’m speaking about. If this is a local script then first place it in the StarterPlayerScripts. Now let’s say if this is a server script. This directly tells us that the code isn’t executing.

Is the value changing in the server? Are you even changing the value, if so then are you changing it locally in a localscript or in a normal server script

Neither, Im changing it manually while the game is running in studio in the explorer

But its not In the GUI, its on a part

That would be the reason. A local script doesnt function in workspace.