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

Value is not counting down despite no errors in output. How do I fix this?

CODE:

local AI = script.Parent.Parent.Timer.PositionNumber

local Lighting = game.ServerStorage.Freddy

local World = game.Workspace.Animatronics.Freddy

local Footstep = script.Parent.Parent.Timer.RemoteFunction.Footstep.Sound


repeat game:GetService("RunService").Stepped:Wait() until AI.Value == 5 


script.Parent.DoorTimer.Value = (math.random(10,15))



	while game.Workspace.DoorL.Closed.Value == true do
		wait(1)
		script.Parent.DoorTimer.Value = script.Parent.DoorTimer.Value -1
	end

So basically when I press a button on a model the boolean value turns true, and pressing it again makes it false. And this script makes it so when something certain happens a counter is randomly set, and having the mentioned BooleanValue set to true should subtract it every second its true. All the setups dont have problems, the BooleanValue changed, the DoorCounter gets randomly set,

But the main problem is getting the counter to go down when the BooleanValue is True

6 comments

0

is the boolean value getting set back to false when the door is closed?

0

it gets set to true when the door is closed and set to false when it isn't.

1

Try this: while game.Workspace.DoorL.Closed.Value == true do wait(1) if script.Parent.DoorTimer.Value == nil then script.Parent.DoorTimer.Value = script.Parent.DoorTimer.Value -1 end end

1

Have you found a solution yet? If so edit the script

1

Finally a hard question for me to solve thanks for posting.

0

hmmmmmmmmmmmmmm

4 answers

✓ Accepted answer

Apparently, the while loops only get(s) called essentially, so you should put the while loop like this. I think the issue was because of the loop, if the while loop gets called even once then it will break the script, and you need to check whether it's not true.

while workspace.DoorL.Closed.Value == true do
if not workspace.DoorL.Closed.Value  then
        wait(1)
        script.Parent.DoorTimer.Value = script.Parent.DoorTimer.Value -1 
    end
end

I have a timer gui script maybe you can use it if you cant find a solution (280 is the amount of seconds)

local time = 280

for i = time, 1, -1 do
    wait(1)
    script.Parent.Text = i
end

1 comment

0

THANKS FOR ACCEPTING! :D

Improvements

Use :GetService() for the ServerStorage and Workspace services

Use :WaitForChild() to make sure an instance exists before using it

Explanation

Once your code gets to the while loop, the condition is checked, however, once that condition is false, then any code after the loop will run and you will be unable to have the loop occur again.

Instead, you can move the condition inside the loop as a if statement and change the condition value to true. Another option is using :GetPropertyChangedSignal() if you need to run any code after the loop. You can also use break to exit out of the loop.

Revised Server Script

local Par = script.Parent
local DTimer = Par:WaitForChild("DoorTimer")
local DPar = Par.Parent
local AI = DPar:WaitForChild("Timer"):WaitForChild("PositionNumber")
local Footstep = DPar:WaitForChild("Timer"):WaitForChild("RemoteFunction"):WaitForChild("Footstep"):WaitForChild("Sound")

local Lighting = game:GetService("ServerStorage"):WaitForChild("Freddy")
local WS = game:GetService("Workspace")
local World = WS:WaitForChild("Animatronics"):WaitForChild("Freddy")
local Closed = WS:WaitForChild("DoorL"):WaitForChild("Closed")

repeat
	game:GetService("RunService").Stepped:Wait()
until AI.Value == 5 

DTimer.Value = (math.random(10, 15))

while true do
	if Closed.Value == true then
		wait(0.99)
		DTimer.Value = DTimer.Value - 1
	end
	wait(0.01) -- to prevent an infinite loop
end

Try using a while true do loop with an if statement saying if the door is closed. If it's closed then break out of the loop.

while true do
	wait(1)
	script.Parent.DoorTimer.Value = script.Parent.DoorTimer.Value -1

	if not game.Workspace.DoorL.Closed.Value then
		break
	end
end

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