How to add a client script later?

I have a script that inserts clientside scripts into StarterPlayerScripts to run once per player. However, if the script takes longer to run than it takes for the player to join, the starter player scripts are copied first, and the new one does not make it to the PlayerScripts. Also I just found out that ReplicatedFirst only replicates once. If a new item is added afterwards it will not be replicated. How can I simply make a client side script that is added later, that actually runs?

This confuses me. So you’re essentially saying you want to replace all LocalScripts of a Player? Just replace them on the explorer.

If I’m wrong, please revise your question as I can’t really understand what you mean, and provide the code you think is problematic.

-- Module Script
script:FindFirstChildOfClass("LocalScript").Parent = game:GetService("StarterPlayer"):FindFirstChildOfClass("StarterPlayerScripts")
-- Server Script
require(asset_id_corrosponding_to_module_script)

This introduces a race condition between a player joining and the starter player scripts being cloned to their player scripts, and the module script running, and thus the local script being parented to starter player scripts. How can I fix this?

I still don’t seem to understand. Why not just modify the player’s PlayerScripts on runtime?

2 Likes

It’s more of a hacky solution to do this, but you parent it to the backpack and in the script it parents itself to PlayerScripts.

How does this solve the problem?

I’m not sure if it solves it in the first place, but since the backpack is replicated, the server can parent it to each player’s backpack. The LocalScript itself parents itself to the PlayerScripts, but that might be problematic.

Why isn’t StarterPlayerScripts a viable option for you?

Nope, a module script invoked from the server, which yields, parents the LocalScript to StarterPlayerScripts. The problem is this sometimes results in bugs if it takes too long to be parented to StarterPlayerScripts, because then it is not copied to the player’s PlayerScripts.

Why exactly does it need to yield?

require(asset_id) yields to load the asset

So, the local script is retrieved by the MainModule?

Yes, the local script is parented to the MainModule, which then parents it to StarterPlayerScripts

Idea 1

You could place these scripts into ReplicatedStorage and they will load.

You need to make them Scripts though and set the RunContext to Client.

Store them somewhere they won’t replicate to clients like ServerStorage, and copy them to ReplicatedStorage in a server script.


Idea 2

Another idea is to parent the latent scripts into a folder in ReplicatedStorage. Then, another LocalScript can use ChildAdded to detect new scripts and move them to PlayerScripts.

1 Like

Idea 1

Perfect! I had no idea of this new feature.

Idea 2

This is what I was thinking, but Idea 1 is better

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