How to clone a local script and make it run immediately after being parented

So, I have this basic local script, but lets assume its just prints hello at its first line. A script from serverscriptservice will clone this local script and parent it to the players player gui. This print statement wont run. I have heard that this is the working solution, but the local script does not run.

I know this line runs fine (server)

game.ReplicatedStorage.CAS_Binder:Clone().Parent = Player.PlayerGui

in RS:
Cas_Binder:
print(“hello”) – this isnt actually the script, but it starts with this statement to see if it runs.

local Game = game
local Players = Game:GetService("Players")
local ServerStorage = Game:GetService("ServerStorage")
local LocalScript = ServerStorage.LocalScript

local function OnPlayerAdded(Player)
	local PlayerGui = Player:FindFirstChildOfClass("PlayerGui") or Player:WaitForChild("PlayerGui", 10)
	if PlayerGui then LocalScript:Clone().Parent = PlayerGui end
end

Players.PlayerAdded:Connect(OnPlayerAdded)

This works for me.

1 Like