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

Am I using GetChildren() correctly?

script.Parent.OnTouch:connect(function()
	for i,p in pairs(script.Parent.Parent.Truck:GetChildren()) do
		for i = 1,200 do
			p.CFrame = p.CFrame * CFrame.new(.4,0,0)
			wait(.1)
		end
	end
end)

For some reason, this script doesn't work. It works with changing size, but not position. There's also this script:

script.Parent.OnTouch:connect(function()
	wait(6)
		for i,p in pairs(script.Parent.Parent.Parts:GetChildren()) do
			for i = 1,10 do
				p.CFrame = p.CFrame * CFrame.new(.25,0,0)
				wait(.1)
			end
		p.Anchored = false
	end
end)

2 comments

0

Keep in mind when you use "GetChildren()". They take ALL the children of the parent. Including scripts and such that do not have positions or CFrame

0

The only things in the models "Truck" and "Parts" are bricks.

1 answer

script.Parent.OnTouch:connect(function()

There is no OnTouch event. That is what people sometimes name their functions that they plan to connect to the Touched event, but the actual event is called Touched, not OnTouch. So,

script.Parent.Touched:connect(function()

should work.

for i,p in pairs(script.Parent.Parent.Parts:GetChildren()) do
            for i = 1,10 do

You have two i variables. I would recommend that you rename one of them to something else.

for index,p in pairs(script.Parent.Parent.Parts:GetChildren()) do
            for i = 1,10 do

I would also recommend that you add a debounce. A debounce is just a bool value that prevents the touched event from being triggered many times at once.

local debounce = false

script.Parent.OnTouch:connect(function()
	if debounce == false then
		debounce = true --Prevents the code from running until it equals false again.
		for i,p in pairs(script.Parent.Parent.Truck:GetChildren()) do
			for i = 1,200 do
				p.CFrame = p.CFrame * CFrame.new(.4,0,0)
				wait(.1)
			end
		end
		debounce = false --Lets it run again
	end
end)

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