Issue With For Loop in Game

I am finishing up making a spleef game and I am trying to delete the tiles when the player steps on them. When the round ends, I want the remaining tiles to be deleted and a whole new set of tiles to replace them when the game restarts.

  • I have already got the tile to delete upon player touched and replace all the tiles at the end of the round, but now the tiles are no longer controlled by my TileControlScript

  • My Issue: once the tiles have been replaced, they no longer trigger the for loop.


Videos:

What is supposed to happen

Screen Recording 2021 02 14 at 8 04 36 PM - YouTube

What happens after the first round

Screen Recording 2021 02 14 at 8 05 43 PM - YouTube


  • What solutions have you tried so far? I did not find solutions for this specific issue.

Here is the script I am using to control the tiles:

local tilesFolder = script.Parent.Tiles

for index, tile in pairs(tilesFolder:GetChildren()) do
	
	tile.Touched:Connect(function(hit)
		
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			
			if tile.Transparency == 0 then
				
				tile.Material = "Neon"
				for i = 0, 1, 0.1 do
					wait(0.1)
					tile.Transparency = i
				end
				
				tile:Destroy()
				
			end
			
			
		end
		
		
	end)
	
	
end

Also, here is the game that I am trying to get this to work in, please note that 2 players are needed to start the round.

Also, I am fairly new to scripting.

The new tiles aren’t working because you destroyed the tiles with the connection. Try reconnecting your new tiles to the function when you replace them.

There are some other solved topics on this, perhaps take a look here:

Additionally, you can use TweenService for a smooth fade

How do I reconnect the new tiles to the function?

I will read this, also, thanks for the TweenService suggestion, I never thought of that.

Is the code that replaces the tiles in the same script? If it is would you mind sending it?

Not a scripter. but have you also thought about just setting CanCollide to false instead of destroying the tile?
I think it would be good enough to remove collision from tiles players step on then give all tiles collision at the start of the round and remove their transparency.

1 Like

No but here is the code that repalces the tiles in the game loop:

-- this runs when the round ends
		
		for i, tiles in ipairs(hotTileFolder:GetChildren()) do
			tiles:Destroy()
		end
		
		for i, tiles in ipairs(coldTileFolder:GetChildren()) do
			local clone = tiles:Clone()
			clone.Parent = hotTileFolder
		end

I recommend doing what @motlopoway said and just setting the cancollide of each tile to false instead of destroying it. This removed the need to clone them and it also preserves the connections you made.

1 Like

When the round starts just run this code again

2 Likes

Yeah, this will be a lot simpler and will fix my problem.

1 Like

Thank you, this will be a lot simpler and will fix my problem.

Update

Turning all the collisions back on at once worked a lot better and fixed it, thank you @motlopoway and @riles0829

What I ended up doing:


-- TileControlScript

local tilesFolder = script.Parent.Tiles

for index, tile in pairs(tilesFolder:GetChildren()) do
	
	tile.Touched:Connect(function(hit)
		
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			
			if tile.Transparency == 0 then
				
				tile.Material = "Neon"
				for i = 0, 1, 0.1 do
					wait(0.1)
					tile.Transparency = i
				end
				tile.CanCollide = false
				
				
			end
			
			
		end
		
		
	end)
	
	
end
-- runs when the round ends

	for i, tiles in ipairs(hotTileFolder:GetChildren()) do
			tiles.Transparency = 0
			tiles.CanCollide = true
		end
2 Likes

Great! I’m glad it works well.

1 Like