Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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) 

1 comment

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.

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.

12 comments

0

Ohh, lemme try that out.

0

Are you sure thats it?

0

Sure as sure can be. Is it not working?

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

0

i just want to know if YOU know what im trying to do

0

Like, can Touched me used for a control seat?

0

Uh, I'm unsure of what you mean. But I'm still certian it will work.

0

be*

0

ok, ill try

0

Your answer didn't work. When I waited for 15 seconds after touching and getting off the seat, then it didn't disappear

0

Er, I was simply trying to fix the error here, not fix the bug. Sorry for the misconception.

0

oh ok sorry

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)

1 comment

0

Thanks man

This discussion is preserved as it was. New answers, comments, and votes are closed.