Touched event doesn't fire on the 2nd child

I recently attempted to script collectable objects locally and make their changes reset when the player dies.
I did this by using a local script in a ScreenGui with ResetOnSpawn enabled, and got the orbs (which have the same name) using In Pairs loop.

local Orbs
for i, v in pairs(workspace:GetChildren()) do --this loop gets the orbs from the workspace and specifies it as Orbs
	if v.Name == "Orb" then
		Orbs = v
	end
end
Orbs.Material = Enum.Material.ForceField
local NavyBlueValue = script["Navy Blue"].Value
local NavyBlueCollected = script["Navy Blue"].Collected
if workspace.OrbPlatform.Transparency ~= 1 then
	local Info = TweenInfo.new(1)
	local Tween = game:GetService("TweenService"):Create(workspace.OrbPlatform,Info,{Transparency=1})
	Tween:Play()
	Tween.Completed:Connect(function()
		workspace.OrbPlatform.CanCollide = true
		workspace.OrbPlatform.CanTouch = true
	end)
end
print(1)
Orbs.Collected.Value = false
NavyBlueCollected.Value = 0
Orbs.Touched:Connect(function(hit) --This is where it stopped after trying to collect the second orb
	print(2)
	print(Orbs)
	local Debounce = true
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Player ~= nil and Humanoid ~= nil and Player == game.Players.LocalPlayer then
		print(3)
		if Humanoid.Health ~= 0 then
			print(4)
			if Player.TeamColor ~= BrickColor.new("Navy blue") then return end
			if Orbs.Collected.Value == false then
				print(5)
				NavyBlueCollected.Changed:Connect(function()
					print(6)
					if NavyBlueCollected.Value == NavyBlueValue then
						print(7)
						if workspace.OrbPlatform.Transparency ~= 0 then
							local Info = TweenInfo.new(1)
							local Tween = game:GetService("TweenService"):Create(workspace.OrbPlatform,Info,{Transparency=0})
							Tween:Play()
							Tween.Completed:Connect(function()
								workspace.OrbPlatform.CanCollide = true
								workspace.OrbPlatform.CanTouch = true
							end)
						end
					else
						print(0)
					end
				end)
				NavyBlueCollected.Value = NavyBlueCollected.Value + 1
				Orbs.Collected.Value = true
				Orbs.Material = Enum.Material.ForceField
			end
		end
	end
end)

Any help is appreciated!

EDIT: I found out it only detects one orb and I don’t know what’s wrong there, because when I collect it first it still doesn’t work. Also ignore debounce for now.

Why you assign Orbs value to one object? I think you should make a list to store them.

You are assigning only 1 orb to the orbs variable, you should make it a table or better off just put the rest of the code inside the loop.

1 Like

I really don’t know any other way to detect the orbs.
In pairs loops was the only way I was able to detect them all on a server script.

When you add orb to list you should do something like this:
Orbs = table.insert(Orbs, v)

1 Like

then after adding orbs to list then you should connect event like this:
for i, v in pairs(Orbs) do
v.Touched:connect([the function])
end

1 Like

Where do I put this? If I put this after local Orbs I get an error because this is an instance.

first you need to add Obrs to list.
Did you do this?
When you do empty list then it should look like this:
local Orbs = {}
Then you should do what i said earlier how to add these orbs to list.

1 Like

I hope you understand what i am saying.

1 Like