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

Why is my timer only waiting 20 seconds instead of 60?

So basically whenever someone guesses the build, it's supposed to destroy it, it does but if no-one can guess it within 60 seconds it's supposed to destroy it. When I guess it correctly multiple times and then I wait it out, it waits 20 seconds instead of 60. 46

When someone guesses it, at line 46 it's supposed to make it so it sets the timer value to Reset and then it triggers in another script.

Main script:

-- Variables -- 

local rs = game:GetService("ReplicatedStorage")
local serverstorage = game:GetService("ServerStorage")
local builds = serverstorage:WaitForChild("Builds")
local status = rs:WaitForChild("Status")
local intermissionlength = 5
local minplayers = 1

-- Script --

while true do
	game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		msg = string.lower(msg)
		if msg == ClonedBuild.Name then
			player.leaderstats.Guesses.Value = player.leaderstats.Guesses.Value + 1
			player.leaderstats.Points.Value = player.leaderstats.Points.Value + 10
			ClonedBuild:Destroy()
			status.Value = player.Name.. " correctly guessed the build. It was ".. ClonedBuild.name.. "!"
			end
			end)
end)
		
		
		status.Value = "Waiting for at least "..minplayers .. " players."
	
		repeat wait() until game.Players.NumPlayers >= minplayers
	
		status.Value = "Intermission..."
	
		wait(intermissionlength)
	
		AvailableBuilds = builds:GetChildren()
	
		ClonedBuild = AvailableBuilds[math.random(1,#AvailableBuilds)]
	
		status.Value = "Guess The Build!"
		
		rs:WaitForChild("Timer").Value = "Run"
	
		ClonedBuild = ClonedBuild:Clone()
		ClonedBuild.Parent = workspace

	
		repeat wait() until game.Workspace:FindFirstChild(ClonedBuild.Name) == nil or rs:WaitForChild("Timer").Value == "Done"
		rs:WaitForChild("Timer").Value = "Reset"
		local b = game.Workspace:FindFirstChild(ClonedBuild.Name)
		if b ~= nil then 
			workspace[ClonedBuild.Name]:Destroy()
			ClonedBuild = nil
			status.Value = "No-one guessed the build in time!"
			wait(3)
		else
			ClonedBuild = nil
			wait(3)
		
		end
		end
		

Timer script:

local rs = game:GetService('ReplicatedStorage')
local timerv = rs:WaitForChild("Timer")
local roundtime = 60

timerv.Changed:Connect(function()
	if timerv.Value == "Run" then   
 		wait(roundtime)
        timerv.Value = "Done"
    elseif timerv.Value == "Reset" then
        print("Reset")

    end
end)

2 comments

0

You shouldn't have PlayerAdded and a Chatted event inside of the while loop as you will make more connections each time the loop goes again.

0

That won't fix the issue though.

1 answer

One main thing you should always consider is breaking your code down into functions. The errors are explained in the comments. Please, for the love of all that is holy, please don't say "It doesn't work". Check your output, I didn't test this. You can fix the errors.

-- Variables -- 

local serverstorage = game:GetService("ServerStorage")
local rs = game:GetService("ReplicatedStorage")
local timer = rs:WaitForChild("Timer") 
local builds = serverstorage:WaitForChild("Builds")
local status = rs:WaitForChild("Status")
local intermissionlength = 5
local minplayers = 1

local gameStarted = false
local roundtime = 60
local chosenMap = false


-- Script --

local function startGame()
	status.Value = 'Intermission'
	wait(intermissionlength)
	local AvailableBuilds = AvailableBuilds:GetChildren()
	local randomBuild = AvailableBuilds[math.random(#AvailableBuilds)]
	chosenMap = randomBuild:Clone()
	status.Value = 'Guess the build'
	chosenMap.Parent = workspace
	gameStarted = true
end


local function stopgame(guessed, player)
	gameStarted = false
	-- no more guessing
	if guessed then 
		status.Value = string.format ("%s correctly guessed the build. It was %s!.", player.Name, chosenMap.Name )
	else
		status.Value = "No-one guessed the build in time!"
	end
	if chosenMap then chosenMap:Destroy() end
	chosenMap = false
	wait(3)
end

-- You don't need this in a seperate script
timer.Changed:Connect(function())
	local t = roundtime
	while gameStarted and t > 0 and timer.Value == 'Run'  then 
		print('Game time: '..t)
		t = t - 1
		wait(1)
	end
	if gameStarted then 
-- make sure we didn't already stop the game
		stopgame (false)
		-- game stopped from time ending or reset
	end
end)


game.Players.PlayerAdded:Connect(function(player))
-- Don't put this in a loop
	player.Chatted:Connect(function(msg))
		if gameStarted and chosenMap then 
			-- Make sure the game is playing and map is chosen
			if msg:lower() == chosenMap.Name:lower() then 
				-- Compare both strings lowered
				local leaderstats = player:FindFirstChild('leaderstats')
				local guesses = leaderstats and leaderstats:FindFirstChild('Guesses')
				local points = leaderstats and leaderstats:FindFirstChild('Points')
				if not leaderstats then print("Can't find leaderstats") return end
				if not guesses then print("Can't find guesses") return end
				if not points then print("Can't find points") return end
				-- Make sure your stats are there

				guesses.Value = guesses.Value + 1
				points.Value = points.Value + 1

				stopgame(true, player)
			end
		end
	end)
end)


while true do
	if not gameStarted  then 
		-- if game hasn't started continue
		local playersplaying = game.Players.NumPlayers
		if playersplaying >= minplayers then 
			startGame()
		else
			print(string.format ( "Wait for %s more players", minplayers - playersplaying))
			-- print how many more players we need
		end
	end
	wait(1)
end

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