Script not functioning correctly

Hey! I was making a round script and it appears to work. However once it goes over 1st round, it starts breaking. I may be missing something and I can’t seem to find the issue
Code:

local HouseFunctions = {}

function HouseFunctions.Function1_StartGame()
	--// Things you can change
	
	
	local Timer = 15 --How long the timer lasts before starting
	
	
	--// Ok no more changing
	
	local Event = game:GetService("ReplicatedStorage").TimerEvents.StartGame.StartTimer
	Event:FireAllClients(Timer)
	local EventReturn = game:GetService("ReplicatedStorage").TimerEvents.StartGame.Return
	EventReturn.OnServerEvent:Wait()
	local PlatesFolder = workspace.Plates
	local Plates = PlatesFolder:GetChildren()
	
	local PlayersService = game:GetService("Players")
	
	local Players = PlayersService:GetPlayers()
	
	for index, value in pairs(Players) do
		print(index)
		value:SetAttribute("PlayedCurrentRound", true)
		value.Team = game:GetService("Teams").Playing
		value.PlayerGui.House.CustomizeButton.Visible = false
		value.PlayerGui.House.HealthBackground.Visible = false
		local PlateToSend = PlatesFolder:FindFirstChild("Plate"..index)
		PlateToSend:SetAttribute("PlayerOwner", tostring(value))
		local CharacterCheck = value.Character or value.CharacterAdded:Wait()
		local Character = value.Character
		local House = PlateToSend.House:GetChildren()
		PlateToSend.Plate.Transparency = 0
		PlateToSend.Plate.CanCollide = true
		for index2, value2 in pairs(House) do
			if value2:IsA("BasePart") then
				value2.CanCollide = true
				value2.Transparency = 0
			end
		end
		Character:MoveTo(PlateToSend.PlayerSpawn.Position)
	end
end

function HouseFunctions.Function2_EndGame()
	--// Things you can change 
	
	
	local EndDelay = 5 --When someone wins, the delay before sending them back and removing the houses
	
	
	--// Ok no more changing
	
	local Event = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.StartTimer
	Event:FireAllClients(EndDelay)
	local EventReturn = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.Return
	EventReturn.OnServerEvent:Wait()
	
	local PlayersService = game:GetService("Players")
	local PlayersTable = PlayersService:GetPlayers()
	
	local PlatesFolder = workspace.Plates
	local PlatesTable = PlatesFolder:GetChildren()
	
	for index, value in pairs(PlatesTable) do
		value:SetAttribute("PlayerOwner", "nil")
		value.Plate.CanCollide = false
		value.Plate.Transparency = 1
		for index2, value2 in pairs(value.House:GetChildren()) do
			if value2:IsA("BasePart") then
				value2.CanCollide = false
				value2.Transparency = 1
			end
		end
	end
	for index, value in pairs(PlayersTable) do
		value.PlayerGui.House.CustomizeButton.Visible = true
		value.PlayerGui.House.HealthBackground.Visible = true
	end
	
	HouseFunctions.Function1_StartGame()
end
return HouseFunctions

Hello,

As in “Breaking”, do you mean that the plates aren’t showing up, the plates are not fixed or normal, or something about some plates being missing from folder?

I think this may have to do with the plates not being cloned from the folder and therefore, those plates in that folder alone are being affected instead of what is expected to be round clones of the plates.

It doesn’t team players correctly and starts even when the delay is happening. The timer also ends up having a stroke and gets mixed up etc.

To be fair, this amount of coding is probably unnecessary. As you could have cloned the plates folder from ReplicatedStorage to game.Workspace. This way, you can still keep the normal setup of the plates and can keep cloning them, then destroying the plates cloned onto game.Workspace at the end of the game.
For example: platesFolder in ReplicatedStorage can be cloned onto workspace, so platesFolder in ReplicatedStorage can still remain normal and unaffected by the round, then you could delete the plates in workspace as a refresh for the round.

Needless to say,

As for timer, may I see what is the script that receives the timer event?

Okay will do. The code to receive the timer events:

local StartEvent = game:GetService("ReplicatedStorage").TimerEvents.StartGame.StartTimer

local StartEvent2 = game:GetService("ReplicatedStorage").TimerEvents.StartGame.Return
StartEvent.OnClientEvent:Connect(function(Timer)
	for number = Timer, 0, -1 do
		script.Parent.Text = "Starting in "..number.." seconds!"
		task.wait(1)
	end
	StartEvent2:FireServer()
end)

local EndEventA = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.StartTimer
local EndReturn = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.Return

EndEventA.OnClientEvent:Connect(function(Timer)
	for number = Timer, 0, -1 do
		script.Parent.Text = "Ending in "..number.." seconds!"
		task.wait(1)
	end
	EndReturn:FireServer()
end)

Already done so at line 58. (Character)

What do you mean the timer is having a “stroke” what it is doing?

2 timers appear to be running at the same time. I was testing it with my friend and after about a round or two they overlap for some reason.

1 Like

And It seems as if the “Playing” team was not unassigned from player when the game ends. The only time you assigned the player to a team was during the startgame function.

There is a separate script which detects when a player dies. When they do, it changes their team.

Try to add a task.wait(EndDelay) after the

local Event = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.StartTimer
Event:FireAllClients(EndDelay)

Let see if the server still would be able to wait 5 seconds before starting.

I don’t understand. It has a second RemoteEvent that fires to the server when at least 1 player is done. I don’t know if you read the script or not but it’s in there.

Ok what part of the delay does not work then? Is it the ending delay or the starting delay?

I think it may be to do with the ending timer.

If the event is waiting for the event to get a signal, then why does it still pass on?

Does it make a difference to add a wait timer onto the server?

Also I made a fix:

local StartEvent = game:GetService("ReplicatedStorage").TimerEvents.StartGame.StartTimer

local StartEvent2 = game:GetService("ReplicatedStorage").TimerEvents.StartGame.Return
StartEvent.OnClientEvent:Connect(function(Timer)
	for number = Timer, 0, -1 do
		script.Parent.Text = "Starting in "..number.." seconds!"
		task.wait(1)
	end
	script.Parent.Text = ''
	StartEvent2:FireServer()
end)

local EndEventA = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.StartTimer

local EndReturn = game:GetService("ReplicatedStorage").TimerEvents.EndGame.A.Return

EndEventA.OnClientEvent:Connect(function(Timer)
	for number = Timer, 0, -1 do
		script.Parent.Text = "Ending in "..number.." seconds!"
		task.wait(1)
	end
	script.Parent.Text = ''
	EndReturn:FireServer()
end)

Alright. When my friend is back, I will test.

Nope. It didn’t fix, here’s a video:
robloxapp-20220119-1451572.wmv (5.2 MB)