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

How do I make that the script would apply to all items in folder?

Asked by
JoneXI 51
4 years ago

Hello, how do I make that script would apply to all the spikes in the folder?

workspace.Spikes.Spike.Touched:Connect(function()

    if not debonce then
        debonce = true
        health.Value = health.Value - 1
        wait(5)
        debonce = false
        end

end)    

Thank you.

2 answers

Log in to vote
0
Answered by 4 years ago

You have to use workspace.Spikes:GetChildren() to get a list of the children of the Spikes model and a for loop to go through each child inside the list of children. Lists in lua are called "tables", so I will refer to them as tables from now.

Here is how you would go through the children inside the Spikes model:

for key, value in pairs(workspace.Spikes:GetChildren()) do
    print(value.Name) -- prints the name of the child
end

key is a number, indicating the order the child is in inside the GetChildren table, and value is the child object itself. We can rename key and value to better names:

for _, spike in pairs(workspace.Spikes:GetChildren()) do
    print(spike.Name) -- prints the name of the child
end

key has been renamed to _ because variables that aren't used are usually named _. value has been renamed to spike because the children are the spike parts.

So here is how you apply your script to all the spikes inside the workspace.Spikes model:

for _, spike in pairs(workspace.Spikes:GetChildren()) do
    local debonce = false
    spike.Touched:Connect(function()
        if not debonce then
            debonce = true
            health.Value = health.Value - 1
            wait(5)
            debonce = false
        end
    end)   
end
0
I like your explanation vChuys 0 — 4y
0
I just commend you for the in depth explanation. Cheers Infocus 144 — 4y
Ad
Log in to vote
0
Answered by
vChuys 0
4 years ago
Edited 4 years ago

This should work if not am sorry but i did it on mobile.

local spikes = workspace.Spikes -- getting the spikes

for i,v in pairs(spikes) do
      v.Touched:Connect(function(hit)
       if debounce == false then
           debounce = true
           if hit.Parent:FindFirstChild("Humanoid) then
           local humanoid = hit.parent:FindFirstChild("Humanoid)
           humanoid:TakeDamage(1)
           wait(3) -- you can change the cooldown
           debounce = false
       end
       end
      end)
end
0
This was made on mobile since its 1 am sorry if i miss spelled anything or did something wrong vChuys 0 — 4y
0
I did an edit check it out vChuys 0 — 4y
0
Thank you. JoneXI 51 — 4y

Answer this question