TextLabel's Text dosen't update even tho the value of it does

Hello, Beginner here, I want to make a simple On/Off Button that changes a label. It’s supposed to represent if the pump is enabled.

My master script needs to know so it constantly checks if the Pump is off or on via the label. It may not be that good performance wise but it’s my first game anyway.

TL;DR I need my label to say Off or On but it always says On even tho print() says the Text value is off

So I change the label from Off to On via the script. However, the odd thing is that it dosen’t “update” the text.
That means the Text Value IS OFF even tho it SHOWS THE PLAYER ON. So it DOES kinda work but the player dosen’t know because it dosen’t say so.

local displayStatus = script.Parent.Parent.Parent.Text

local clickDetector = script.Parent


if displayStatus == "Off" then
	script.Parent.Parent.BrickColor=BrickColor.new("Really red")
else
	script.Parent.Parent.BrickColor=BrickColor.new("Lime green")
end


function onMouseClick()
	
	print(displayStatus)
	
	if displayStatus == "On" then
		displayStatus =  "Off"
		script.Parent.Parent.BrickColor=BrickColor.new("Really red")

	elseif displayStatus == "Off" then
		displayStatus =  "On"
		script.Parent.Parent.BrickColor=BrickColor.new("Lime green")
		
	end	
	
	print(displayStatus)
	
end

clickDetector.MouseClick:connect(onMouseClick)

It’s a bit diffilcult to explain, but basically in-game the button says Off even tho it’s value is On (says print).
My hierachy looks something like this:

Edit: Ok, another problem. When my main script checks the text variable it always thinks the button is On (as the label says) even tho my script that does the on/off stuff says otherwise. ahhhh

edit and reference the text label directly rather than using the displaystatus variable

1 Like

I would set up the pump like this:
-edit
For some reason I can’t upload the image to show the structure so I’ll just have to write it and hope you understand it:

Pump01
= Button
=== Button Script
=== Click Detector
= SurfaceGui
=== DisplayStatus

and this would be the script code:

local button=script.Parent
local clickDetector=button.ClickDetector
local pump=button.Parent
local displayStatus=pump:WaitForChild("SurfaceGui").DisplayStatus

-- Set initial Status
displayStatus.Text = "Off" 


function onMouseClick()
	
	print(displayStatus.Text)
	
	if displayStatus.Text == "On" then
		displayStatus.Text =  "Off"
		pump.BrickColor=BrickColor.new("Really red")

	elseif displayStatus.Text == "Off" then
		displayStatus.Text =  "On"
		pump.BrickColor=BrickColor.new("Lime green")
		
	end	
	
	print(displayStatus.Text)
	
end

clickDetector.MouseClick:connect(onMouseClick)
1 Like

You need to set displayStatus to script.Parent.Parent.Parent then you can set displayStatus.Text

otherwise it wouldnt work

1 Like

Thanks alot @luaNerd, it worked perfectly fine for me!

Also thanks alot @ZombieCrunchUK for your awnser, it helps me understand how the children system works, a bigger problem for me right now is the organization of my stuff haha, also I uploaded the image to imgur and then posted the link to it here

2 Likes