Value isn't changing

Hey! I tried to do a round system, but when i change the value to start a timer, when i want to make the countdown, the value dont change, it stays in the number. Any solution?

My code, Its a ServerScript in ServerScriptService

local CounterValue = 0
local Stopped = false

GameValues.CounterTime:GetPropertyChangedSignal("Value"):Connect(function()
	CounterValue = GameValues.CounterTime.Value
end)

GameValues.CounterStopped:GetPropertyChangedSignal("Value"):Connect(function()
	Stopped = GameValues.CounterStopped.Value
end)

function selectGame()
	local GameSelected = math.random(1, #Games)
	if table.find(GamesSelected, GameSelected) ~= nil then
		repeat
			GameSelected = math.random(1,#Games)
		until table.find(GamesSelected, GameSelected) == nil
	end
	print(Games[GameSelected])
	table.clear(GamesSelected)
	table.insert(GamesSelected, GameSelected)
end

function StartCounter()
	for i = CounterValue, 0, -1 do
		GameValues.CounterTime.Value = CounterValue
		if Stopped == true then
			repeat wait() until GameValues.CounterStopped.Value == false
		end
	end
end

while true do
        wait(10)
	StartCounter()
end

try changing the counter timer value to the loop index

for Index = CounterValue, 0, -1 do
		GameValues.CounterTime.Value = Index
		if Stopped == true then
			repeat wait() until GameValues.CounterStopped.Value == false
		end
	end

Yeah, It works, But i get this error, What do i need to do?

ServerScriptService.GameMixBase:48: invalid 'for' initial value (number expected, got nil)

Is from this line

GameValues.CounterTime:GetPropertyChangedSignal("Value"):Connect(function()
	CounterValue = GameValues.CounterTime.Value
end)

Many issues with this code that you printed it all out. The main issue is at the end where you do your for loop I assume you want to use the index I.

Hey. So there are quite a few things getting in the way here. The biggest problem is as soon as you start your game, the code skips through all the good stuff and goes straight to the while true do loop.

From that point on there is no way it will ever check if a GameValue changed because it is stuck in the while true do loop forever. So here’s what I did.

First of all I don’t know where you are storing your “GameValues” folder, so I set mine to game.Workspace.GameValues. Keep that in mind since if yours is in a different location you will need to change that in your script or it wont work.

First, make a new script in ServerScriptService and copy paste the following

local timerStopped = game.Workspace.GameValues.CounterStopped

wait(25)
timerStopped.Value = false

This is just to change the bool value you called “Stopped” after 25 seconds.
so your repeat until loop can recieve a new value.

now here is my version of your code. I added comments to hopefully help you understand what I did and why.

local CounterValue = 0
local GameValues = game.Workspace.GameValues    --added 
local Stopped = GameValues.CounterStopped  -- changed stopped to the bool value that you named "CounterStopped"
CounterTime = GameValues.CounterTime   -- added

function StartCounter(CounterValue)  -- added parameter
	for i = CounterValue, 0, -1 do
		print(CounterValue)
		task.wait(1)  -- added so it counts down once every second
		CounterValue = CounterValue - 1  -- added so the CounterValue actually changes each loop
		
		if CounterValue == 0 then  -- added so the for loop doesn't go into negatives
			Stopped.Value = true
			if Stopped.Value == true then
				print("stopped")
				repeat wait() until Stopped.Value == false
			end
-- this line below wont print until the bool value "Stopped" = false, which we set up in the other script to happen after 25 second
			print("Timer Stopped Value is False")
		end
	--	GameValues.CounterTime.Value = CounterValue  -- commented out, because not needed
	end
end

--[[ over here I took your ":Connect(function() line" and moved it below the StartCounter function, because we will need to pass the timers set time through the function, it will need to be done this way, especially if your different games have different times]]-- 

-- there is no need for a while true do loop because this function below is constantly checking for your CounterTime value to change
GameValues.CounterTime:GetPropertyChangedSignal("Value"):Connect(function()
	CounterValue = CounterTime.Value -- took GameValues out of this line, no need since we declared it up top
	StartCounter(CounterValue)  -- added CounterValue parameter so we can run the time you set it to through the function
end)

wait(4)
CounterTime.Value = 10  -- notice how I changed the value through script, it wont work if you do it manually

--[[                   -- not sure exactly how you want to incorporate this so i commented it out
function selectGame()
	local GameSelected = math.random(1, #Games)
	if table.find(GamesSelected, GameSelected) ~= nil then
		repeat
			GameSelected = math.random(1,#Games)
		until table.find(GamesSelected, GameSelected) == nil
	end
	print(Games[GameSelected])
	table.clear(GamesSelected)
	table.insert(GamesSelected, GameSelected)
end
]]--

You can just copy paste the codes if you like but you’ll need to put your GameValues folder in Workspace. Otherwise make sure to change the location in your script.

I hope this helps to answer your questions. Anything else I can do to help let me know.
I’m fairly new to this too and helping others helps me to learn lots.

Oh , and also try to get used to naming your created variables in Camel Case.
ex.
local counterValue = 0
without the capital on the first letter. It makes things less confusing even for you.
keep all your folder names and values named with the capitals though
ex. CounterStopped ,which is the name of the bool value you made

this allows you to visually separate your created variables from the other stuff.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.