Issue with changing Label Text via Script

  1. What do you want to achieve? I am attempting to change a label’s text after a TextButton is pressed.

  2. What is the issue? The StringValue (VPNConnected) in ReplicatedStorage will change (code below), but the label text won’t.

  3. What solutions have you tried so far? I have looked for solutions on the Developer Hub, but have not found anything. I have also tried changing it from a LocalScript to a regular Script to see if that would do anything.

local value = game:GetService('ReplicatedStorage').VPNConnected
local Label = script.Parent.Parent.VPNStatus

script.Parent.MouseButton1Click:Connect(function()
	if value.Value == "No" then
		value.Value = "Yes"
	else
		value.Value = "No"
	end
end)

if value.Value == "Yes" then
	Label.Text = "You are connected"
	Label.TextColor3 = Color3.new(0, 1, 0)
else
	Label.Text = "You are not connected"
	Label.TextColor3 = Color3.new(1, 0, 0.0156863)
end

I apologize if my code isn’t that good, I am not fluent in Lua/Luau.


(video low quality lol recorded on roblox studio)

the problem here is that you aren’t checking if the value changed. the code just runs once at the start and then never checks it again. you could just change the text within the if statement within the function. this also should be a localscript

local value = game:GetService('ReplicatedStorage').VPNConnected
local Label = script.Parent.Parent.VPNStatus

script.Parent.MouseButton1Click:Connect(function()
	if value.Value == "No" then
		value.Value = "Yes"
        Label.Text = "You are connected"
	    Label.TextColor3 = Color3.new(0, 1, 0)
	else
		value.Value = "No"
        Label.Text = "You are not connected"
	    Label.TextColor3 = Color3.new(1, 0, 0.0156863)
	end
end)
3 Likes

you should do what @bossay6 said
the value isn’t being switched in the example (ignore this line i just didn’t see it)

so instead of this

if value.Value == "No" then
	value.Value = "Yes"
else
	value.Value = "No"
end

you can do a 1 liner

-- if it's 'No' then switch to 'Yes', otherwise switch to 'No'
VPN.Value = (VPN.Value == "No") and "Yes" or "No" 
local VPN = game:GetService('ReplicatedStorage').VPNConnected -- rename to avoid repitition
local Label = script.Parent.Parent.VPNStatus

local function ToggleVPN()
    VPN.Value = (VPN.Value == "No") and "Yes" or "No" 
	if VPN.Value == "Yes" then
		Label.Text = "You are connected"
		Label.TextColor3 = Color3.new(0, 1, 0)
	else
		Label.Text = "You are not connected"
		Label.TextColor3 = Color3.new(1, 0, 0.0156863)
	end
end

script.Parent.MouseButton1Click:Connect(ToggleVPN)
2 Likes

#solved #solved #solved #solved #solved

2 Likes

If your trying to make a script that just changes the button then just do this simply.

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Text = “What you want”
end)

1 Like

it’s supposed to be a script that toggles a VPN
so changing the text one time wouldn’t be right

1 Like

Use debounce! if your not changing the value using a server anyway if you are changing using a server use remote events

2 Likes

Thank you @bossay6 & @BonesIsUseless for the explanations! Both scripts worked and I understood what the problem was.
Have a great day :heart: :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.