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

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

Asked by 5 years ago

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

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

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
0
yeah should work starmaq 1290 — 5y
0
for line 29 you can also use "If v.ClassName == 'BasePart' then" Fad99 286 — 5y
0
ok ill try this testing34545 12 — 5y
0
BasePart is not a class name you would not be able to do it that way. vissequ 105 — 5y
0
yes i had to change that testing34545 12 — 5y
Ad

Answer this question