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

GetChildren() function not working properly ?

so basically I made this simple script that should trigger a shotgun when a part is touched. it will make a bunch of little parts visible making it look like shogun fire, but the script spawns in every part individually every 1 second not all in once

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then	
		local all = script.Parent.Parent:GetChildren()
		for i, v in pairs(all) do
		 if v.Name == "Bullet" then
			v.Transparency = 0
			wait(1)
			v.Transparency = 1
		 end	
		end
	end 
end

script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

The reason it waits for a second when touched is that when using in pairs(), it finds each part individually instead of instantly. So by waiting for a second, you're delaying the rest of the script from firing. This can be easily fixed by adding a delay, which instead of delaying the entire script, delays a function.

function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) then   
        local all = script.Parent.Parent:GetChildren()
        for i, v in pairs(all) do
         if v.Name == "Bullet" then
            v.Transparency = 0
            delay(1,function()
            v.Transparency = 1
			end)
         end    
        end
    end 
end

script.Parent.Touched:connect(onTouch)

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