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

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

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

✓ Accepted answer

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

2 comments

0

I like your explanation

0

I just commend you for the in depth explanation. Cheers

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

3 comments

0

This was made on mobile since its 1 am sorry if i miss spelled anything or did something wrong

0

I did an edit check it out

0

Thank you.

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