Can someone help me make this work?

I’m trying to make a script that only runs after a value it made is set to true. So this is what I came up with:

local Loaded = Instance.new("BoolValue", ReplicatedFirst)
Loaded.Name = "Loaded" Loaded.Value = false
repeat task.wait() until Loaded.Value == true
print("works")

But the output never returns anything, even when the value is set to true. What am I doing wrong?

Ok,

  1. NEVER use the Second Argument for Instance.new()

  2. repeat would be unessacary and would use way more resources.
    Instead use a Changed Event to perform all of this:

  3. BoolValues automatically default to false with their Value, so there is no purpose to make sure they are false.

local Loaded = Instance.new("BoolValue") -- Instance
Loaded.Name = "Loaded" -- Name
Loaded.Changed:connect(function(boolean) -- Changed Event
    if boolean then -- if boolean is true
        -- code
    end

end)
Loaded.Parent = ReplicatedFirst -- Parents

Assuming this is logic that is only run once (hence the “loaded”), I’d recommend using a BindableEvent
(assuming the client manages when to change the flag). You’d probably just want to have it already inside of ReplicatedFirst since there’s no real benefit to creating a new one rather than just having it already there. Here’s what I’d do:

local LoadBindable = game:GetService("ReplicatedFirst"):WaitForChild("Loaded")

LoadBindable.Event:Wait()
-- Assuming it only runs once and you'll never need it again, might as well destroy it locally:
LoadBindable:Destroy()
-- Rest of loading logic

If it can run multiple times, you could use :Connect() rather than :Wait():

local LoadBindable = game:GetService("ReplicatedFirst"):WaitForChild("Loaded")

LoadBindable.Event:Connect(function()
-- Rest of loading logic
end)

When your other script finishes loading all of the necessary resources, you can simply do this:

local LoadBindable = game:GetService("ReplicatedFirst"):WaitForChild("Loaded")

LoadBindable:Fire()

You generally want to avoid polling (repeating until a value is true) as it’s typically pretty hard on performance compared to events.

I’ve pasted this code in:

local Loaded = Instance.new("BoolValue")
Loaded.Name = "Loaded"
Loaded.Changed:Connect(function(Boolean)
	if Boolean then
		print("Works")
	end
end)
Loaded.Parent = ReplicatedFirst

So pretty much the exact code.
But it doesn’t look like the output is returning anything, and yes the value is getting changed to true.

Is it a problem that the event is being fired by a LocalScript and the the script that I’m trying to get to print is a normal script?

If the Value of the Boolean is being changed by the Client, the server wont see it.

You can fire a remote to the server and the server responds to it by changing the value or just doing what u wanted it to do after changing.

In this case, you’d have to use a RemoteEvent. Creating instances such as BoolValues on the client will not replicate over to the server for security reasons. If you want to send a message to the server saying loading is done, you could try this:

-- No reason to use ReplicatedFirst here, any delay would be negligible
local LoadEvent = game:GetService("ReplicatedStorage"):WaitForChild("Loaded")

-- Once everything is loaded:
LoadEvent:FireServer()

Then, on a server script, you’d receive the event:

local LoadEvent = game:GetService("ReplicatedStorage"):WaitForChild("Loaded")

LoadEvent.OnServerEvent:Connect(function(Player)
-- Loading logic
end)

Keep in mind that exploiters can spam events, so you may want to make a table of players that have already used it and prevent them from using it again (make sure to clear players that leave out of the table).

It’s because the value wasnt defined ur connecting to nothing

local Loaded = Instance.new(“BoolValue”) – Instance
Loaded.Name = “Loaded” – Name
Loaded.Parent = game.ReplicatedFirst – Parents
Loaded:GetPropertyChangedSignal(“Value”):Connect(function()
print (Loaded.Value)
end)

how do you set the value? because if you’re doing it manually from the explorer without changing to server view it will only be client sided and not work whatever you do. There’s also a lot more efficient ways you can do this, like @DasKairo said but if you only want to run it once I’d suggest changing it to

Loaded:GetPropertyChangedSignal("Value"):Wait()

it will yield the script until the value of loaded is changed

This worked beautifully! I’ll work on that anti-cheat to prevent exploiters. Thank you so much!

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