Scripts Aren't working with multiple parts of the same name

I am making a collection function in a game where items can randomly spawn or have at least multiple on a map and players can take the items and give them ownership of the item. I have the item in replicated storage and just for testing purposes I have a button that I press that clones the item into a folder into the workspace, but when I spawn the item, the collection script itself does not work at all.

Here is the collection Script (it’s a local script)


local players = game:GetService("Players")

local EggOwnership = game.Players.LocalPlayer:WaitForChild("Eggs"):WaitForChild("Egg1")
local ui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("EggGot")

local EggFolder = game.Workspace.Eggs


for _, descendant in pairs(EggFolder:GetDescendants()) do
	if descendant.Name == "MoonEgg" then
		descendant.Touched:Connect(function(player)
			warn("egg touched")
			descendant:Destroy()
			warn("egg destroyed")
			game.Workspace.EggRemotes.Egg1Get:FireServer()
			warn("Fired")


			if EggOwnership.Value == false then


				ui.Visible = true


				--ui.Position = UDim2.new(0.5,0,1.5,0)
				ui:TweenSizeAndPosition(UDim2.new(0.5,0,0.5,0),
					UDim2.new(0.5,0,0.5,0),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Bounce,
					.5,
					false)


				wait(5)




				ui:TweenSizeAndPosition(UDim2.new(0.01,0,0.01,0),
					UDim2.new(0.5,0,1.5,0),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Bounce,
					1,
					false)





				wait(1)

				ui.Visible = false



			else

				if EggOwnership.Value == true then

					warn("Already Has Egg1!")

				end
			end

		end)
	end
end

The item in Replicated Storage
image

I get no errors, but I have made those print tests above to see where the script ends up but none of the prints show, so it just stops at some point.

I am trying to get the script to where I can spawn multiple items on a map, and it will work and let multiple players collect the items even when there are multiple on a map. It should show a Congratulations GUI if the player hasn’t already collected the item. I also have it fire a remote event that will change a Boolean value inside the player that represents ownership of that same item, so that if the Boolean is set to true, it wont show a GUI or change the Value.

1 Like

The for loop only consists of the eggs that are originally inside of the EggFolder. If you add more eggs to them, then it wouldn’t account for those. You’ll have to use DescendantAdded to detect whenever an egg is added and add the same line of code. Like so:

local players = game:GetService("Players")

local EggOwnership = game.Players.LocalPlayer:WaitForChild("Eggs"):WaitForChild("Egg1")
local ui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("EggGot")

local EggFolder = game.Workspace.Eggs

local function EggTouch(descendant)
    if descendant.Name == "MoonEgg" then
		descendant.Touched:Connect(function(player)
			warn("egg touched")
			descendant:Destroy()
			warn("egg destroyed")
			game.Workspace.EggRemotes.Egg1Get:FireServer()
			warn("Fired")


			if EggOwnership.Value == false then


				ui.Visible = true


				--ui.Position = UDim2.new(0.5,0,1.5,0)
				ui:TweenSizeAndPosition(UDim2.new(0.5,0,0.5,0),
					UDim2.new(0.5,0,0.5,0),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Bounce,
					.5,
					false)


				wait(5)




				ui:TweenSizeAndPosition(UDim2.new(0.01,0,0.01,0),
					UDim2.new(0.5,0,1.5,0),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Bounce,
					1,
					false)





				wait(1)

				ui.Visible = false



			else

				if EggOwnership.Value == true then

					warn("Already Has Egg1!")

				end
			end

		end)
	end
end

for _, descendant in pairs(EggFolder:GetDescendants()) do
    EggTouch(descendant)
end

EggFolder.DescendantAdded:Connect(EggTouch)
2 Likes

That makes more sense. I’ll Read into DescendantAdded. Also, the script works well now, thank you very much.

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