Help using a touched event for multiple parts

I’m trying to change the lighting of the game locally so only the players client is affected, but my script only works for one part at a time. I’ve tried to group the “SkyChanger” parts in the workspace and create a table and run with that but then my script does nothing but doesn’t error so I don’t know what’s wrong with it.

local Lighting = game.Lighting
local SkyFolder = game.ReplicatedStorage.Lighting
local debounce = false

local part = game.Workspace:WaitForChild("SkyChanger")

part.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(touched.Parent)
		if player then
			if player == game.Players.LocalPlayer then
				if debounce == false then
					debounce = true
					
					game.Lighting:ClearAllChildren()
					task.wait(0.5)
					
					local getSky = SkyFolder:FindFirstChild(part.Selection.Value)
					for i, v in pairs(getSky:GetChildren()) do
						local cloneSky = v:Clone()
						cloneSky.Parent = game.Lighting
					end
					task.wait(5)
					debounce = false
				end
			end
		end
	end
end)

This is the version of my script that actually works, however obviously it will only work for the 1st “SkyChanger” part found in the workspace. Any tips on making this script work for multiple parts? (Sorry i’m new to scripting)

Hello, Im not sure if this will fix your problem but instead of using multiple scripts for multiple parts I suggest using CollectionService. You can read about it more here if youre interested… CollectionService | Documentation - Roblox Creator Hub

I would also recommend using this plugin that can add tags to parts and stuff using collectionservice. The plugin is right here.

If you need help I suggest watching VectorThree’s video on this plugin. Its quick and easy to understand.

6 Likes

Wow thank you so much I had no idea this even existed!

1 Like

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