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.
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.
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.
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.
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.
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