Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does a certain function restart after timer hits zero?

This is a script for a new game that i'm working on. It starts to check for players whenever two or more players enter the game, then the timer starts running. The problem is that whenever the timer hits zero, the enterplr function restarts. what seems to be the problem?

Data = game.Workspace.GameData
TimeLeft = game.Workspace.GameData.Timer
Players = game.Players
Plr_amnt = Data.Plrs

function enter(plr)
	wait()
	Plr_amnt.Value = Players.NumPlayers
end

function check()
	wait(2)
	if Plr_amnt.Value >= 1 then
		gamestart()
	elseif Plr_amnt.Value == 1 then
		print("Retrying...")
		wait(2)
		check()
	end
	end


function gamestart()
	TimeLeft.Value = 50
	for i=1,50 do
		wait(1)
		TimeLeft.Value = TimeLeft.Value - 1
	end
end

while wait(enter()) do
check()
end

game.Players.PlayerAdded:connect(enter)

1 comment

0

GameData is a folder and TimeLeft, Plr_amount are numvalues

1 answer

✓ Accepted answer

Here is the updated Code. It is LocalScript I only realized the flaw of the script after I was fully done. You will notice that the Player1's output is different from Player2 output. You would have to change the following script into Server script but I think you are more than capabale enough to do so.

-- DeclarationS Section
-- //Game Services  
local Workspace = game:GetService("Workspace");
local Player = game:GetService("Players");
local PlayerList = Player:GetChildren();
local PlayerAmount = #PlayerList;

-- //Game Data and its Number Values 
local GameData = Workspace:WaitForChild("GameData");
local DataValue = GameData:WaitForChild("DataValue");
local PlayerAmountValue = GameData:WaitForChild("PlayerAmountValue");

-- //Variables 
local MinimumPlayerCount = 1;
local PremierTime = 50; 
local isRoundFinished = false;
local AmountOfWaitTime = 10;

-- Processing Section 

-- //Start Round 
function BeginRound ()
	print("Envoked Function");
	DataValue.Value = PremierTime;
	isRoundFinished = false;
	
	if isRoundFinished == false then 
		for i = PremierTime, 1, -1 do
			DataValue.Value = i;
			print(DataValue.Value);
			wait(1);
		end;
		
		isRoundFinished = true; 
		
		wait(AmountOfWaitTime);	

		-- This is where you add you Minigame code
		spawn(MinigameCode)	
		spawn(CountAmountOfPlayers);
	else 
		wait(AmountOfWaitTime);
		print(AmountOfWaitTime);
		
		isRoundFinished = false;
	end;
end;

-- //Counting #Players 
function CountAmountOfPlayers ()
	print("Envoked CountAmountPlayers Function");
	PlayerAmountValue.Value = PlayerAmount; 
	wait();
	
	if PlayerAmountValue.Value > MinimumPlayerCount then 
		print("Enough Player");
		spawn(BeginRound);
	elseif PlayerAmountValue.Value <= MinimumPlayerCount then 
		print("Not Enough Players");
		wait(2);
		spawn(CountAmountOfPlayers);
	end
end;


-- Connecting Section 
Player.PlayerAdded:Connect(CountAmountOfPlayers)

Sorry the video got cut off but you can still check the code if you want to.

This discussion is preserved as it was. New answers, comments, and votes are closed.