Help With Getting Parts

This script detects when a part is touched and then gives the player how touched it speed boost. I have a lot of PowerUp parts all over workspace inside all different folders. Is there a way to make one script that can make all of them work by using one script.

local speedBoostPart = script.Parent
local speedBoostDuration = 2
local speedMultiplier = 2
local cooldownTime = 5
local canBoost = true

local function onTouched(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if humanoid and canBoost then
        local originalSpeed = humanoid.WalkSpeed

        humanoid.WalkSpeed = originalSpeed * speedMultiplier

        -- Change the part's surface material to SmoothPlastic
        speedBoostPart.Material = Enum.Material.SmoothPlastic

        canBoost = false

        wait(speedBoostDuration)

        humanoid.WalkSpeed = originalSpeed

        -- Reset the part's surface material
        speedBoostPart.Material = Enum.Material.Smooth

        wait(cooldownTime)

        canBoost = true
    end
end

speedBoostPart.Touched:Connect(onTouched)

You could apply a tag for all the parts and using CollectionService:

local SpeedBoostParts = CollectionService:GetTagged("SpeedBoost") -- the unique tag id

local function onTouched(speedBoostPart, hit)

local canBoost = true 

-- ... more code here

end

for _, speedBoostPart in SpeedBoostParts do
speedBoostPart.Touched:Connect(function(hit)

onTouched(speedBoostPart, hit)

end)
end

though you need to move some of the variables into the onTouched function (canBoost and speedBoostPart)

Is there a another way to do it or is that the only way. I am fine doing it but I just have to got tag every PowerUp part

Of course there is (most alternatives uses similar techniques to do but with folders), but this is my usual approach to doing such task as it is convenient (for me)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.