Hello. I am creating a morphing system for a round based system when a random player gets selected. After the player gets morphed, I fire a remote event to give the player special abilities. The script receiving the remote event is located in StarterGui, which seems to be reset when morphing the character. This script isn’t loading fast enough to receive the remote event when fired. Currently, to deal with this problem, I am using task.wait() for half a second, which I imagine is a very bad way to approach this. I am wondering if there is a way to solve my problem. I imagine the possible solutions are to wait for the script in StarterGui to fully load or to not reset StarterGui when morphing the character, but I’m not sure how to go about this.
This is the main server script where I morph the character. As soon as this function is completed, I fire the remote event later in the script.
function morphChosenPlayer(player)
local data = PlayerDataModule.GetData(player)
local equippedSkin = data["EquippedSkin"]
local skinInfo = Skins[equippedSkin]
local oldCharacter = player.Character
local newCharacter = player.Model:Clone()
newCharacter.HumanoidRootPart.Anchored = false
newCharacter:PivotTo(oldCharacter.PrimaryPart.CFrame)
player.Character = newCharacter
newCharacter.Parent = workspace
for i, charScript in pairs(game:GetService("StarterPlayer").StarterCharacterScripts:GetChildren()) do
local charScriptClone = charScript:Clone()
charScriptClone.Parent = newCharacter
end
oldCharacter:Destroy()
task.wait(0.5) -- My current method of waiting, not preferred
end
The script receiving the event is a local script in StarterGui. All it does is listen for the fired event. The current system right not works, but I’m sure it is not practical. Any help is appreciated, thanks.