Scripts breaking when cloned and no errors

I am trying to make a platform system simular to squid games and I need some help.

Whenever the timer hit 0 and the platforms get reset the collection service script does not work. whenever I start up the game it works because of the wait but whenever I get cloned a second time after the timer is up then it stops working and there are no errors aswell. Any help is greatly appriciated to help countinue my beginner scripting journey.

Countdown Script

local countdownValue = 1 * 60  -- Initial countdown value in seconds (6 minutes)
local countdownSpeed = 1  -- Speed of the countdown (1 second per second)
local Workspace = game:GetService('Workspace')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local function updateCountdown()
	countdownValue = countdownValue - countdownSpeed
end

local function formatTime(seconds)
	local minutes = math.floor(seconds / 60)
	local remainingSeconds = seconds % 60
	return string.format("%d:%02d", minutes, remainingSeconds)
end

local function resetPlatforms()
    local PlatformFolder = Workspace.Bridge.Platforms
    for _, object in ipairs(PlatformFolder:GetChildren()) do
        object:Destroy()
    end

    local ReplicatedChildren = ReplicatedStorage.Platforms:GetChildren()
    for i, originalObject in ipairs(ReplicatedChildren) do
        local cloneObject = originalObject:Clone()
        cloneObject.Parent = PlatformFolder
        cloneObject.Name = "PlatformClone_" .. tostring(i % 100 + 1)
    end
end


local function chooseFake()
	local PlatformFolder = workspace.Bridge.Platforms
	local children = PlatformFolder:GetChildren()

	for _, Platform in ipairs(children) do
		if Platform:IsA('Model') or Platform:IsA('Folder') then
			local subChildren = Platform:GetChildren()

			local usedValues = {}  -- Keep track of used Fake values within the same subChildren

			for _, subPlatform in ipairs(subChildren) do
				if subPlatform:IsA('Part') then
					local fakeProperty = subPlatform:FindFirstChild("Fake")
					if fakeProperty then
						local randomValue

						repeat
							randomValue = math.random(2)  -- Get a random number either 1 or 2
						until not usedValues[randomValue]

						usedValues[randomValue] = true
						fakeProperty.Value = (randomValue == 1)
						
					else
						
					end
				end
			end
		else
			
		end
	end
end






-- Initial setup
resetPlatforms()
chooseFake()

while wait(1) do
	updateCountdown()

	if countdownValue >= 0 then
		-- Broadcast the countdown value to all clients
		game:GetService("ReplicatedStorage"):WaitForChild("CountdownEvent"):FireAllClients(countdownValue)
	else
		print("Countdown has ended!")
		resetPlatforms()
		chooseFake()
		countdownValue = 6 * 60  -- Reset countdown to 6 minutes
	end
end

Collection script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")

wait(1)

for _, Platform in pairs(CollectionService:GetTagged("Platform")) do
	local WorkspaceCheck = Platform.Parent.Parent.Parent.Parent:FindFirstChild("ReplicatedStorage")
	if WorkspaceCheck then
		-- Skip the current Platform and continue with the next one
		continue
	end

	local function onButtonTouched(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local player = Players:GetPlayerFromCharacter(hit.Parent)

		if humanoid and player then
			ReplicatedStorage:WaitForChild("PlatformEvent"):FireClient(player, Platform)
		end
	end

	Platform.Touched:Connect(onButtonTouched)
end

1 Like

You are only looping through the initial Platforms, you need to listen to new ones with CollectionService:GetInstanceAddedSignal

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")

local function NewPlatform(Platform)
	local WorkspaceCheck = 	Platform.Parent.Parent.Parent.Parent:FindFirstChild("ReplicatedStorage")
	if WorkspaceCheck then
		-- Skip the current Platform and continue with the next one
		continue
	end

	local function onButtonTouched(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local player = Players:GetPlayerFromCharacter(hit.Parent)

		if humanoid and player then
			ReplicatedStorage:WaitForChild("PlatformEvent"):FireClient(player, Platform)
		end
	end

	Platform.Touched:Connect(onButtonTouched)
end

CollectionService:GetInstanceAddedSignal("Platform"):Connect(NewPlatform)

for _, Platform in pairs(CollectionService:GetTagged("Platform")) do
	NewPlatform(Platform)
end

This should also allow you to remove the wait

2 Likes

Thank you, I new it had to be something with collection service because I just started it week ago. Thanks for your time!

1 Like

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