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

GetChildren() function not working properly ?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
Ad

Answer this question