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.
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
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