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

How to repeat a function on touch?

First off, I'm new to lua and doing my best so please be gentle, secondly I always search around before asking so I apologise in advance if I've missed an obvious solution elsewhere on the site/internet!

I have a section of a game I'm building that spawns some boulders at the top of a slope and lets gravity take it from there, setting them to auto destroy on a timer is unreliable given that they can sometimes take longer to get to the bottom than at other times. I've put a part at the bottom which I will make transparent but want to run a script when any part named "boulder" makes contact with it to destroy the boulder (3 can make contact in quick succession at times...).

So far I have (again, I'm new, please withhold laughter!):

local boulder = workspace.boulder

local function boulder1(hit)

boulder:Destroy()

end

script.Parent.Touched:connect(boulder1)

which works fine for the first one to make contact... how can I repeat the function on each and every hit?

Thanks for any help.

1 answer

✓ Accepted answer

You can use FindFirstChild() to detect what has hit the part:

local function boulder1()
	if script.Parent:FindFirstChild("Boulder") then -- FindFirstChild() never returns false
		local boulder = script.Parent:FindFirstChild("Boulder")
		boulder:Destroy()
	end
end
script.Parent.Touched:Connect(boulder1) -- Use Connect() not connect()

8 comments

0

This runs every time something hits the part

0

Thanks, I actually understand that (the annotation helps!). I'm still having problems, i think it's line 3 as the boulders are spawned and exist in the workspace only...am I right in thinking that this looks for a child called "Boulder" in the parent of the script, in this case the boulder collision detection brick? Sorry again for my ignorance!

0

line 3 only detects if the part that hit the part with the script is named "Boulder", it doesn't detect anything that doesn't hit the part

0

ok great, I get it now... I'd misplaced a line further on in the full script and now everything works as intended. Thanks so much for your help

0

Final question, how do I mark an answer as THE answer?! Can't find a way to accept an answer lol

0

click "Accept Answer"

0

I expected as much but don't seem to have an 'accept answer' button anywhere lol, do I need 25 rep like for upvoting? [edit] never mind, logging out and in again allowed the popup to show :) Thanks

0

yes

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