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

How to repeat a function on touch?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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()
0
This runs every time something hits the part DeceptiveCaster 3761 — 5y
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! GrumpyTheDaddy 17 — 5y
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 DeceptiveCaster 3761 — 5y
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 GrumpyTheDaddy 17 — 5y
View all comments (4 more)
0
Final question, how do I mark an answer as THE answer?! Can't find a way to accept an answer lol GrumpyTheDaddy 17 — 5y
0
click "Accept Answer" DeceptiveCaster 3761 — 5y
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 GrumpyTheDaddy 17 — 5y
0
yes DeceptiveCaster 3761 — 5y
Ad

Answer this question