Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

0
is the boolean value getting set back to false when the door is closed? Jonix151 65 — 4y
0
it gets set to true when the door is closed and set to false when it isn't. Adenandpuppy 87 — 4y
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 JesseSong 3916 — 4y
1
Have you found a solution yet? If so edit the script JesseSong 3916 — 4y
View all comments (2 more)
1
Finally a hard question for me to solve thanks for posting. JesseSong 3916 — 4y
0
hmmmmmmmmmmmmmm coolmanHDMI 72 — 4y

4 answers

Log in to vote
4
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 3 years ago

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
Ad
Log in to vote
1
Answered by 4 years ago

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
0
THANKS FOR ACCEPTING! :D coolmanHDMI 72 — 4y
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question