Script won't update switch

  1. What do you want to achieve?
    I’m trying to make a rail/track switch that switches based on a boolean value
  2. What is the issue?
    When clicking the button, nothing happens at all even though it should change the value which then changes the track direction/switch
  3. What solutions have you tried so far?
    Youtube (with no result) and chatgpt (also with no good result)

Script:

local curve = script.Parent.Curve
local rails20m = script.Parent.Rails20m

function changeCollisionAndTransparency(object, canCollide, transparency)
	object.CanCollide = canCollide
	object.Transparency = transparency
end

function updateModelsUnderParent(parent, canCollide, transparency)
	local children = parent:GetDescendants()
	for _, child in ipairs(children) do
		if child:IsA("BasePart") then
			changeCollisionAndTransparency(child, canCollide, transparency)
		end
	end
end

function updateSwitch(newState)
	script.Parent.State.Value = newState
	updateModelsUnderParent(curve, not newState, newState and 0.75 or 0)
	updateModelsUnderParent(rails20m, newState, newState and 0 or 0.75)
end

script.Parent.State.ValueChanged:Connect(updateSwitch)

-- Optional: In case I still want the button to change the state when clicked (for debugging stuffs)
local function onButtonClick()
	local newState = not script.Parent.State.Value
	updateSwitch(newState)
end

script.Parent.Show.Click.MouseClick:Connect(onButtonClick)

image
(switchcontroller is the script above)

1 Like

Shouldn’t it be State.Changed?

Not sure…
Just updated the script and nothing still happens

This most definitely should be script.Parent.State.Changed:Connect(updateSwitch). Since State is a BoolValue object, you need to use the .Changed event in order to properly update the switch.

1 Like

Wow, it works! Thanks!
I see the problem now too, this is the first time I’m using this forum lol.
Appreciated the help!

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