How to run this local script on multiple parts?

I made a script that makes it when you touch a checkpoint, it changes the checkpoint color and emits some particles to show that the user has pressed it. It’s a local script in StarterPlayerScripts. Instead of making 50 local scripts that do the same thing for each checkpoint, is there a way to make the script work on all checkpoints?

Script:

local checkpoint = game.Workspace.Checkpoints["1"]
-- particle emiters
local a = checkpoint.Confetti.a
local b = checkpoint.Confetti.b
local c = checkpoint.Confetti.c
local d = checkpoint.Confetti.d
local e = checkpoint.Confetti.e
-- Checkpoint Outlines
local a1 = checkpoint.a
local b1 = checkpoint.b
local c1= checkpoint.c
local d1 = checkpoint.d

debounce = true

function onTouched(hit)
	
	if debounce == true then
		debounce = false
		a.Enabled = true
		b.Enabled = true
		c.Enabled = true
		d.Enabled = true
		e.Enabled = true
		
		a1.BrickColor = BrickColor.new("Lime green")
		b1.BrickColor = BrickColor.new("Lime green")
		c1.BrickColor = BrickColor.new("Lime green")
		d1.BrickColor = BrickColor.new("Lime green")
		checkpoint.Arrow.StageArrow.Texture = "http://www.roblox.com/asset/?id=14157770728"
		
		wait(0.15)
		a.Enabled = false
		b.Enabled = false
		c.Enabled = false
		d.Enabled = false
		e.Enabled = false
	end

end 
checkpoint.Touched:connect(onTouched)

All checkpoints are in a folder in the workspace called “Checkpoints” (each checkpoint is named with its checkpoint number. Ex. “1”)
image

Video of what the script does:

5 Likes

You should look into CollectionService. It allows you to tag specific parts and then iterate through them easily

So you could do something like this

local CS = game:GetService("CollectionService")
for i,checkpoint in pairs(CS:GetTagged("checkpoint")) do 
	-- particle emiters
	local a = checkpoint.Confetti.a
	local b = checkpoint.Confetti.b
	local c = checkpoint.Confetti.c
	local d = checkpoint.Confetti.d
	local e = checkpoint.Confetti.e
	-- Checkpoint Outlines
	local a1 = checkpoint.a
	local b1 = checkpoint.b
	local c1= checkpoint.c
	local d1 = checkpoint.d
	debounce = true
	function onTouched(hit)
		if debounce == true then
			debounce = false
			a.Enabled = true
			b.Enabled = true
			c.Enabled = true
			d.Enabled = true
			e.Enabled = true
			a1.BrickColor = BrickColor.new("Lime green")
			b1.BrickColor = BrickColor.new("Lime green")
			c1.BrickColor = BrickColor.new("Lime green")
			d1.BrickColor = BrickColor.new("Lime green")
			checkpoint.Arrow.StageArrow.Texture = "http://www.roblox.com/asset/?id=14157770728"
			wait(0.15)
			a.Enabled = false
			b.Enabled = false
			c.Enabled = false
			d.Enabled = false
			e.Enabled = false
		end
	end 
	checkpoint.Touched:connect(onTouched)
end

(note I didnt test the code but I think this should work)

All you would need is to place this into a single script and it should do the trick

3 Likes

Hey, thanks for the help! This seems like it would work but I did notice that the checkpoint/checkpoint folder isn’t defined, where should I define it so the script will work properly?

1 Like

Its not a folder but a “tag” instead you can tag an instance with <CollectionService>:AddTag(instance, tagName)
so for example game:GetService("CollectionService"):AddTag(game.Workspace.checkpoint1, "checkpoint")

Though manually tagging stuff like that might be a bit difficult but there is also this plugin that lets you tag stuff in your game more easily
Tag Editor Plugin

Hmm, I just tried the script, and it doesn’t seem to be working. I’m unfortunately not familiar with CollectionService. Do I need to change anything manually?

Download a plugin called tag editor

The script’s purpose is to change color when the checkpoint is pressed, It wouldn’t make sense for it to be changed globally when someone presses a checkpoint.

1 Like

Another way you could do this is by just iterating through the folder like this but I usually prefer to use CollectionService for stuff like this

this script should work for what you needed

for i,checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do 
	-- particle emiters
	local a = checkpoint.Confetti.a
	local b = checkpoint.Confetti.b
	local c = checkpoint.Confetti.c
	local d = checkpoint.Confetti.d
	local e = checkpoint.Confetti.e
	-- Checkpoint Outlines
	local a1 = checkpoint.a
	local b1 = checkpoint.b
	local c1= checkpoint.c
	local d1 = checkpoint.d

	debounce = true

	function onTouched(hit)

		if debounce == true then
			debounce = false
			a.Enabled = true
			b.Enabled = true
			c.Enabled = true
			d.Enabled = true
			e.Enabled = true

			a1.BrickColor = BrickColor.new("Lime green")
			b1.BrickColor = BrickColor.new("Lime green")
			c1.BrickColor = BrickColor.new("Lime green")
			d1.BrickColor = BrickColor.new("Lime green")
			checkpoint.Arrow.StageArrow.Texture = "http://www.roblox.com/asset/?id=14157770728"

			wait(0.15)
			a.Enabled = false
			b.Enabled = false
			c.Enabled = false
			d.Enabled = false
			e.Enabled = false
		end

	end 
	checkpoint.Touched:connect(onTouched)
end
1 Like

Use this video to lear about collection service

Is there any benefit using CollectionService?

In a scenario like this maybe not so much, but for example I usually use CollectionService to keep stuff organized that doesn’t really go into a specific folder (ex: several doors that around a map that need to be scripted)

1 Like

Oh, I got it! The script seems to be working perfectly. Thanks for the help and teaching me a bit about Tags!

Hey just one more thing, just tried with multiple checkpoints and the debounce is only allowing it to work once (on the first checkpoint) then not working on the others. Is it possible to keep the debounce to the particles dont emit more than 0.15 sec? (when I took the debounce out it worked perfectly fine just the particles emited constantly, which didn’t look to great)

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