Ad
Log in to vote
2

What is the correct way of triggering code if a part is touched?

Asked by
Xyternal 145
25 days ago

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!

3 answers

Log in to vote
1
Answered by 25 days ago

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 :Connect.

0
Ohh, lemme try that out. Xyternal 145 — 25d
0
Are you sure thats it? Xyternal 145 — 25d
0
Sure as sure can be. Is it not working? deeskaalstickman649 394 — 25d
0
haven't tried it yet, but something i know is that whenever i get someting wrong, it has a deeper problem. I'll try in a bit Xyternal 145 — 25d
View all comments (8 more)
0
i just want to know if YOU know what im trying to do Xyternal 145 — 25d
0
Like, can Touched me used for a control seat? Xyternal 145 — 25d
0
Uh, I'm unsure of what you mean. But I'm still certian it will work. deeskaalstickman649 394 — 25d
0
be* Xyternal 145 — 25d
0
ok, ill try Xyternal 145 — 25d
0
Your answer didn't work. When I waited for 15 seconds after touching and getting off the seat, then it didn't disappear Xyternal 145 — 25d
0
Er, I was simply trying to fix the error here, not fix the bug. Sorry for the misconception. deeskaalstickman649 394 — 25d
0
oh ok sorry Xyternal 145 — 16d
Ad
Log in to vote
1
Answered by 25 days ago

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) 
0
Did you test it out? What the code should do, is that if the player sat on the seat, and then got of it, and doesn't get back on in 30 seconds, then the tank should get despawned. But if the player touches the seat just in time, then it should break from the loop, and prevent the tank from getting despawned. Xyternal 145 — 25d
Log in to vote
1
Answered by
Xapelize 1835 Moderation Voter
17 days ago
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)
0
Thanks man Xyternal 145 — 16d

Answer this question