-
I am trying to maintain the state of a bridge in my script (bridgeState). I have set up two functions, one to Activate the bridge (activateBridge()) and another de-activate it (deactivateBridge()).
-
The default script settings have the bridgeState set to “false”
-
When I touch the Part (button) the conditional print statement runs, and the bridge activates. In addition, the bridgeState changes to true
-
When I touch the button again, nothing happens. It simply fires the Activate() bridge function again.
- Can someone help explain to me why the conditional expression is not re-evaluated once the state changes?
local button = script.Parent
local bridge = game.Workspace.Bridge
button.Anchored = true
button.BrickColor = BrickColor.new("Bright green")
local bridgeState = false
local function activateBridge()
print("Button is now active!")
button.BrickColor = BrickColor.new("Really red")
bridge.Transparency = 0
bridge.CanCollide = true
bridgeState = true
end
local function deactivateBridge()
print("Bridge is now off!")
button.BrickColor = BrickColor.new("Bright green")
bridge.Transparency = .5
bridge.CanCollide = false
bridgeState = false
end
if bridgeState == false then
button.Touched:Connect(activateBridge)
print("Player pressed the button! bridgeState is now " .. tostring(bridgeState))
end
if bridgeState == true then
button.Touched:Connect(deactivateBridge())
print("Player pressed to turn off. bridgeState is now " .. tostring(bridgeState))
end