LocalScript doesn't load in time

So I have a game that when ever a group of player joins the script will instantly fire a remote event for all clients, but because of that the localscripts are apparently not loaded yet in time, I came to this conclusion because when ever I added a “wait(3)” it would work. But relying on such a method doesn’t seem like the best option to me. Does anybody know a better solution to this? I have tried to find one myself, but couldn’t find one yet.

Use :WaitForChild() because it waits until the instance is loaded (or becomes a child of the said instance)

game.Players.PlayerAdded:Connect() or :WaitForChild()

I can’t use that on the playergui where my local script is in since it’s server side, the player loads in perfectly fine but the gui apparently doesn’t load in time.

Why can’t you use it on the server, nothing’s stopping you. (Except for :WaitForChild() hehehehhehehhe)

Huh, where do you want me to use WaitForChild on, since if I do that on the player nothing will change.

WaitForChild on every descendant until you get to the local script, so you can fire the event as soon as it loads.

I guess a very easy solution to your problem would be to place the LocalScript with the OnClientEvent connection inside of ReplicatedFirst

The localscript is in a gui though.

You can do this:
Screenshot 2023-12-17 152910
Then do this inside of the LocalScript:

local screenGui = script:WaitForChild"ScreenGui"
screenGui.Parent = game.Players.LocalPlayer:WaitForChild"PlayerGui"

game.ReplicatedStorage:WaitForChild"RemoteEvent".OnClientEvent:Connect(print)
1 Like

The game may take times to load so adding 30 as 2nd argument of your WaitForChild namecall would be something to do. Tho i most case it will be okay but we never know if the client take times to load :man_shrugging:

Of course. For the start the ‘Wait()’ function is deprecated, better use library task, task.wait(). You also could use more advanced and better way with task.delay(). But better way would be a:

if not game:IsLoaded() then
    game.Loaded:Wait()
end

or

local Players = game:GetService('Players')
Players.PlayerAdded:Wait; -- or you could use a connect/once event with your logic

That’s not how WaitForChild works. The 2nd argument for WaitForChild is a time-out so if you do for example :WaitForChild(“ScreenGui”, 30) it will wait 30 seconds for the ScreenGui to load then it will stop waiting after 30 seconds whether the ScreenGui has loaded or not. Just doing :WaitForChild"ScreenGui" will wait indefinitely until ScreenGui loads no matter how long it takes for it to do so

This seemed to work for me, tbh never rlly used replicatedfirst before but now it seems so handy, if I just knew that earlier. Anyways thx for the help again lol.

1 Like

Wait, but now it doesn’t show the gui in game :((

I don’t know if you knew that but the default timeout isnt infinite look at this
image

Infinite yield == infinite. That’s a warning that the script is waiting indefinitely for something that might not exist

nvm I messed up it works now xd

1 Like

It should work, I tested it and the Gui did show up for me

2 Likes

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