Greetings! I wrote a piece of code to raise and lower a bridge when an object is in a zone and chose the most unreliable method to do so. I understand there are better ways like Region3 but applying it is rather rough. I need some insight on this code. If there are sensors in the zone, the bridge is to lower and stay lowered until there are no more sensors in said zone.
The bridge is raising and lowering continuously as sensors enter the zone and that is not what I want. Any help is best help. Apologies if any of this is confusing. I have trouble learning things and explaining it.
local Zone = script.Parent
local Winch = script.Parent.Parent.Model.RopePart.RopeConstraint
local Up = script.Parent.Parent.Up
local Down = script.Parent.Parent.Down
local debounce = false -- bridge lifted
Zone.Touched:Connect(function(TrainInZone)
if not TrainInZone:FindFirstChild("TrainSensor") then
print("Bridge Found No Sensors")
return
end
if debounce == false then
print("Bridge Lowering")
Winch.Parent.Parent.Base.Anchored = false
Winch.WinchTarget = 112.072
debounce = true
if Winch.CurrentDistance == 112.072 then
Winch.Parent.Parent.Base.Anchored = true
end
end
end)
Zone.TouchEnded:Connect(function(TrainLeftZone)
if not TrainLeftZone:FindFirstChild("TrainSensor") then
print("Bridge Found No Sensors")
return
end
if debounce == true then
print("Bridge Lifting")
Winch.Parent.Parent.Base.Anchored = false
Winch.WinchTarget = 50
debounce = false
if Winch.CurrentDistance == 50 then
Winch.Parent.Parent.Base.Anchored = true
end
end
end)
ignore the Up and Down as they were meant for bugchecking but never used it because of print. I could alternatively put the bridge on a timer but I need it more so on demand than anything.
Wouldn’t recommend using TouchEnded, like ever, it is extremely unreliable as it fires even if the player is still in the part as you mentioned. An alternative is ZonePlus, it uses Roblox’s Spatial Query API and isn’t performance heavy.
local zone = Zone.fromRegion(zoneCFrame, zoneSize)
zone.itemEntered:Connect(function(item)
print(("%s entered the zone!"):format(item.Name))
end)
zone.itemExited:Connect(function(item)
print(("%s exited the zone!"):format(item.Name))
end)
local Zone = script.Parent
local Winch = script.Parent.Parent.Model.RopePart.RopeConstraint
local Up = script.Parent.Parent.Up
local Down = script.Parent.Parent.Down
local debounce = false -- bridge lifted
Zone.Touched:Connect(function(TrainInZone)
if not TrainInZone:FindFirstChild("TrainSensor") then
print("Bridge Found No Sensors")
return
end
if debounce == false then
print("Bridge Lowering")
Winch.Parent.Parent.Base.Anchored = false
Winch.WinchTarget = 112.072
debounce = true
if Winch.Length == 112 then
wait(4)
Winch.WinchTarget = 50
debounce = false
end
end
end)
can you explain or help me figure out why this isnt working? I am trying to make an alternative on a timed loop to avoid this advanced stuff for the time being because using the Region3 looks really advanced for me right now with little explanations.
Im legit about to give up because any help is not even given.
local Zone = script.Parent
local Winch = script.Parent.Parent.Model.RopePart.RopeConstraint
local Up = script.Parent.Parent.Up
local Down = script.Parent.Parent.Down
local length1 = 114
local length2 = 50
local TG = 110
local debounce = false -- bridge lifted
Zone.Touched:Connect(function(TrainInZone)
if not TrainInZone:FindFirstChild("TrainSensor") then
print("Bridge Found No Sensors")
return
end
if debounce == false then
debounce = true
print("Bridge Lowering")
Winch.Parent.Parent.Base.Anchored = false
Winch.WinchTarget = length1
print("Im down")
print(Winch.CurrentDistance)
elseif TG <= Winch.CurrentDistance or Winch.Length == 110 then
print(Winch.CurrentDistance)
print("Going up")
wait(4)
Winch.WinchTarget = length2
debounce = false
print("Bridge Raised")
end
end)