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

why isn't this script that turns a value to true not working?

So i'm trying to make a script where after a random amount of seconds, a value will turn to true, then after that, after a random amount of seconds, that value turns false.

This script is in server script service, and the value is in replicated storage

local values = game.ReplicatedStorage.Values
local state = values.State.Value
	
while state == false do
wait(math.random(500, 1000)) --500,1000
state = true
end


while state == true do
wait(math.random(300, 600)) --300,600
state = false
end
	

2 answers

✓ Accepted answer
local state = values.State.Value

The variable state is storing the value of the values.State BoolValue, so changing the variable state does not change the .Value property of the values.State BoolValue.

What you should instead do is set values.State.Value to whatever value whenever you want to change it like so:

local state = values.State
state.Value = true

state is now the values.State BoolValue object, so you can set its .Value property to true by doing state.Value = true

Here's what your code should look like:

local values = game.ReplicatedStorage.Values
local state = values.State

while state.Value == false do
wait(math.random(500, 1000)) --500,1000
state.Value = true
end


while state.Value == true do
wait(math.random(300, 600)) --300,600
state.Value = false
end

One thing to note is that wait(x) waits x amount of seconds, so wait(math.random(500, 1000)) will wait randomly between 500 to 1000 seconds, or in other words 8.33 minutes to 16.66 minutes. You should set the wait time to a lower amount, like 5 seconds, to test things out.

You cannot allocate properties into variables. Lua Compiler will understand that you’re asking to store the current state instead of make a reference to the Value property. Essentially, your Script is storing State.Value quite literally, your vairable translates to:

local State = true

You’ll then be modifying that Boolean variable. To actually modify a property, you’ll have to call it within the respective line:

local values = game.ReplicatedStorage.Values
local state = values.State

while state == false do
    wait(math.random(500, 1000)) --500,1000
    state.Value = true
end


while state == true do
    wait(math.random(300, 600)) --300,600
    state.Value = false
end

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