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

Is there a way to detect when a child of a model is touched?

So I have a game called Blow Blocks Up! an I have decided to add points into the game. I'm trying to make it so that if someone shoots a rocket at a block they will earn 10 points. Here's my code:

local parent = script.Parent

local player

function onChildTouched(hit)

if hit.name == "Rocket" then

player = hit.creator.Value

game.Players:GetPlayerFromCharacter(workspace[player]).leaderstats.Points.Value = game.Players:GetPlayerFromCharacter(workspace[player]).leaderstats.Points.Value + 10

end

end

-- add a detection for when a child is touched

alright this is my game https://www.roblox.com/games/2850428770/Blow-Blocks-Up-Black-Hole OK i know this script might make it like Destruction Simulator but I still want it anyway.

1 answer

✓ Accepted answer

I've tested this and it works (although I don't have the leaderstats set up on my end)

It iterates through the parts in the model to see is they are parts and if they are parts it connects your function with the touched event.

local parent = script.Parent



local player

  

function onChildTouched(hit)

if hit and hit.Name == "Rocket" then

player = hit.creator.Value

game.Players:GetPlayerFromCharacter(workspace[player]).leaderstats.Points.Value = game.Players:GetPlayerFromCharacter(workspace[player]).leaderstats.Points.Value + 10

  

end

  

end

  

for i,v in pairs(parent:GetChildren()) do

if v:IsA("BasePart") then

v.Touched:Connect(onChildTouched)

end

end

5 comments

0

yeah should work

0

for line 29 you can also use "If v.ClassName == 'BasePart' then"

0

ok ill try this

0

BasePart is not a class name you would not be able to do it that way.

0

yes i had to change that

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