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.
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()