Hello everyone, I was creating a little script where if someone touches the seat again, then the loop stops. So I added a little if statement that goes like this.
local seat = script.Parent seat.TouchEnded:Connect(function()) local count = 0 while count < 30 do if seat.Touched then break end if count == 30 then game.Workspace.Tank:Destroy() end count = count + 1 wait(0.5) end end)
So I think I have done a little weird mistake, like i'm doubting the line that says "if seat.Touched then". Please help me with my script. Thanks!
The issue here is the wrapping of the function. An arguments list ends when a
) is added. If you look at the line where the function gets connected in the event's
:Connect function, you'll see the arguments list end before the first line of the code you want to wrap. Everything else is fine, you simply need to remove the
) of the
Hey i think this will be the answer to your question. Also your code looks messy but you can tidy them up by doing like i did.
local seat = script.Parent local Break = false seat.Touched:Connect(function() -- Sets up an event that sets break to true Break = true end seat.TouchEnded:Connect(function()) local count = 0 Break = false -- Resets the value when you leave the seat while count < 30 do if Break then -- breaks from the loop if Break = true break end if count == 30 then game.Workspace.Tank:Destroy() end count = count + 1 wait(0.5) end end)
local Seat = script.Parent local Count = 30 local IsTouching = false seat.Touched:Connect(function() Count = 30 -- Reset time IsTouching = true end) seat.TouchEnded:Connect(function() IsTouching = false -- Make it false while wait(1) do -- Every 1 second if IsTouching == false then -- Detect if IsTouching == false, if not it obviously does not pass the statement Count -= 1 -- Minus one end end Count.Changed:Connect(function() -- Count changed if Count <= 0 then -- If count equal or lower than 0 game.Workspace.Tank:Destroy() -- Destroy tank end end end)