Script not responding to boolvalue

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want my script to respond to a boolvalue when it becomes true
  2. What is the issue? Include screenshots / videos if possible!
    It does not respond
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    The devforum did not give me solutions

Here is the script, it is a server script inside of a model.

local zombieSpawner = script.Parent
local roundInProgress = game.ReplicatedStorage.Values.RoundInProgress
local playingTeams = game.Teams.Playing

while zombieSpawner.Parent.Parent == workspace and roundInProgress.Value == true do
	print(roundInProgress.Value)
	local zombie = game:GetService("ServerStorage")[" "]:Clone()
	zombie.HumanoidRootPart.Position = zombieSpawner.Containment.Position + Vector3.new(0, 0, -5)
	zombie.Parent = game:GetService("Workspace")
	
	local spawnCooldown = 7
	local playerCount = #playingTeams:GetChildren() - 1
	local currentCooldown
	
	if spawnCooldown - playerCount <= 3 then
		currentCooldown = 3
	else
		currentCooldown = spawnCooldown - playerCount
	end
	wait(currentCooldown) -- Change how much time between each zombie spawn
end

When (roundInProgress == true) was removed from the script, the script printed roundInProgress as true so it acknowledged roundInProgress as true, but it does not start the loop even when roundInProgress becomes true.

2 Likes

That’s because the loop is already executed(first line is a condition) and when checked, it doesn’t really do the job. For a guaranteed execution of all the lines for at least once, use repeat (lines) until (condition).

If you want to re-run the loop, consider using functions with functional programming styles. You need to contain the main loop within a function and execute whenever needed.

1 Like

What do you mean by the loop is already executed, thus not doing the job? I am confused. Do you mean that after the script checks the conditions for the while statement, since it is false, it never checks it ever again?

1 Like

It could be that “zombieSpawner.Parent.Parent” may not be workspace? Or, it could be that you are setting “roundInProgress” to true in a local script.

1 Like

What I’m saying is that the script you wrote only does something once. Never twice. If the compound condition of zombieSpawner.Parent.Parent == workspace and roundInProgress.Value == true is false, the entire loop is skipped entirely.

Thanks! I tried your solution and it is fixed.

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