Check if a local is changed

I’m not really sure on how .Changed works but I presume you can’t use that (I’m relatively new to scripting so I could 100% be wrong) so if i did

local h = "a"

script.Parent.MouseButton1Click:Connect(function()
       h = "b"
end)

how would I detect if h was changed?

Best thing I can think of is making “h” into something such as a StringValue allowing you to use :GetPropertyChangedSignal() on it

I’ll try doing that and see if it works

My take would be checking each x amount of seconds if the value has changed.

You could also just set an Attribute of the script named h script:SetAttribute(“h”,“a”)

and use :GetAttributeChangedSignal

I think the good way is to make a value and through the script make a :GetPropertyChangedSignal() in this way you will get when its value changes and the following conditionals…

From docs:

local part = Instance.new("Part")
-- Save the current state
local currentColor = part.BrickColor
local function onBrickColorChanged()
local newColor = part.BrickColor
print("Color changed from " .. currentColor.Name .. " to " .. newColor.Name)
currentColor = newColor
end

part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)

-- Make some changes
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Really blue")

Yeah but a local is way different to a part inside of workspace

1 Like

You must do it from the server script with the ‘FireEvent’, what you need is that when you click that happens and it is reflected in the server, right? (for all)

I can’t do it from a server script as it’s a localscript which has specific options per player

You could use a RemoteEvent to say what player the value needs changing for

The only way to detect a change of a local variable is to use a while loop that checks if the value has changed

local h = "a"
local old = "a"

script.Parent.MouseButton1Click:Connect(function()
       h = "b"
end)

while h ~= old do
    old = h

    --Do other things here
end

Note that code afeter the while loop will not run until the while loop is stopped.

Maybe you could detect it by doing a function:

local runService = game:GetService("RunService")

-- Detect if change function
local function detectTextChanged(text, textValueOption)
	
	runService:BindToRenderStep('CheckValue', 1, function()
		if text ~= textValueOption then
			-- do something
			warn('Is not equal')
		end
			
		if text == textValueOption then
			-- do something
			warn('is equal')
		end	
	end)
end

-- calling function:
local myText = "Hello"

detectTextChanged(myText, 'Hello') -- arguments here

-- I've thought of something like that, I'm not sure if it's what you need to do

Oops! forget, all this inside a while true or run service
RunService (roblox.com)

CODE UPDATED

I just updated the code, check if it works