This script (which is in Workspace) is supposed to check the IntValue inside a model as well as the color of a part inside it. Once the time reaches a certain point and the brick color is at the correct color, it is supposed to activate a separate script in workspace that causes a transition in lighting. Once the requirements are met, the loop is supposed to break.
The problem is, it’s doing nothing. Nothing is appearing in the Output either.
I know for a fact that the script it is enabling works just fine, it just isn’t being enabled. I’ve tried removing the break and tried removing the part color checking statement to just make this an if then statement checking the IntValue. Am I not able to put an if statement in a while true do loop? If not, how would I go about achieving what I’m aiming for?
local Time = game.Workspace.CapturePoint.TimeHeld.Value
local Team = game.Workspace.CapturePoint.LightPart.BrickColor
local Transition = game.Workspace.Transition
while true do
if Time == 30 and Team == "Bright blue" then
Transition.Disabled = false
break
end
wait(5)
end
You already defined the time value at the start of the script so its always the same thus the script will not run since it is not “30” as the script only reads it once
Two Ways to Counter This
-- keep defining Time in the loop
while true do
local Time = game.Workspace.CapturePoint.TimeHeld.Value
local Team = game.Workspace.CapturePoint.LightPart.BrickColor
local Transition = workspace.Transition
if Time == 30 and Team == "Bright blue" then
Transition.Disabled = false
break
end
wait(5)
end
Second Way
--Script will check for the value by Finding Time's value and Team's BrickColor everytime it compares
local Time = game.Workspace.CapturePoint.TimeHeld--dont define it just yet
local Team = game.Workspace.CapturePoint.LightPart--dont define it just yet too
local Transition = game.Workspace.Transition
while true do
if Time.Value == 30 and Team.BrickColor == "Bright blue" then
Transition.Disabled = false
break
end
wait(5)
end
You can use a repeat loop for this instead. A repeat loop will repeat the code block inside until defined conditions are met. So in other words, it’s exactly what you are trying to do. You can replace your while loop like this:
repeat
task.wait(5) -- uses the new 'task' library
until Time >= 30 and Team == "Bright blue"
Transition.Disabled = false
Also, a while loop is meant to repeat as long as a certain condition is true. It is basically the older sibling of a repeat loop. Your code using the while loop should look like this:
while Time.Value >= 30 or Team.BrickColor ~= "Bright blue" do
task.wait(5)
end
Transition.Disabled = false
As you can see, you can use either loop to accomplish the same thing. However, just for code readability, it’s best to use each for their intended purposes, such as use a while only as long as a certain condition remains the same and a repeat until a condition is met.
Edit: As @dukzae mentioned, depending on how you implemented your system the time value may exceed what you expect. It’s usually good code practice to always assume that the time can exceed what you expect and use a >= sign. Taking this into account, your code should be:
local Time = game.Workspace.CapturePoint.TimeHeld
local Team = game.Workspace.CapturePoint.LightPart.BrickColor
local Transition = game.Workspace.Transition
repeat
task.wait(5)
until Time.Value >= 30 and Team == "Bright blue"
--Time.Value -= 30 --Additionally, depending on what you need, you can account for the time value exceeding your expectations by adjusting it to the value that it exceeded 30
Transition.Disabled = false
You can’t guarantee that the time will be 30. Instead of checking if the time is equal to 30, check if it’s equal to or greater than 30.
Time >= 30 --instead of:
Time == 30
Additionally, Time is a static value because you referenced its value instead of just the instance at the top of your script. You need to reference the instance in the variable and the value in the loop.
Actually I was just copying @itsmedefectiveblocky’s original code. I assume they know how their system works and what conditions they need, so I’m not going to change it. The only times I change someones code is when they are using deprecated methods (like how I changed the original wait(5) to task.wait(5)) or if there is a better way of doing something.
If their code relies on the behavior of only continuing if both conditions are met, then I could break it by making it so it only continues when one condition is met. In this scenario it truly wouldn’t be that bad. They would just have to change the or back to and. But if they were creating a bigger system and I assumed I knew more about how their system worked just from a code sample they provided, then depending on how many conditions they have it could be painful to change them all back to how they originally were.
My bad, I should have explained better. Your first code is fine, where you use repeat loops.
However in the second code, you switch the symbols from == to ~= and by doing so, you also need to switch and to or.
He wants Transition disabled as soon as both conditions are met. However, your while loop will only run until one of them is met, not both. It needs to be or.
Thank you and everyone else for your help. I used the repeat until tactic in the script. I took the defined BrickColor out of the variables and put it as part of the repeat loop. When tinkering around with it and used ~=, I noticed that it worked, but also worked when the red team was in possession of the capture point, since I guess the code thought it was close enough. From there I found out the issue must’ve been with the way I was defining the color, so I messed around with it.
Turns out, I was missing a BrickColor.new(“Bright blue” after the Team.BrickColor ==. After that, it worked. I wouldn’t of thought to of turned my time value to a >= either. The reason I had the wait time at 5 seconds was because I figure it’d put less stress on the server.
local Time = game.Workspace.CapturePoint.TimeHeld
local Team = game.Workspace.CapturePoint.LightPart
local Transition = game.Workspace.Transition
repeat
task.wait(5)
until Time.Value >= 30 and Team.BrickColor == BrickColor.new("Bright blue")
Transition.Disabled = false