Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by
Xyternal 247 Moderation Voter
2 years 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 2 years 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 247 — 2y
0
Are you sure thats it? Xyternal 247 — 2y
0
Sure as sure can be. Is it not working? deeskaalstickman649 475 — 2y
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 247 — 2y
View all comments (8 more)
0
i just want to know if YOU know what im trying to do Xyternal 247 — 2y
0
Like, can Touched me used for a control seat? Xyternal 247 — 2y
0
Uh, I'm unsure of what you mean. But I'm still certian it will work. deeskaalstickman649 475 — 2y
0
be* Xyternal 247 — 2y
0
ok, ill try Xyternal 247 — 2y
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 247 — 2y
0
Er, I was simply trying to fix the error here, not fix the bug. Sorry for the misconception. deeskaalstickman649 475 — 2y
0
oh ok sorry Xyternal 247 — 2y
Ad
Log in to vote
1
Answered by 2 years 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 247 — 2y
Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years 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 247 — 2y

Answer this question