I’m trying to make it such that the weapon’s indicator in my game updates to base off what weapons in their inventory a player has whenever they spawn and therefore fire the Player.CharacterAdded
event.
The template for the weapon indicator is as below:
The hierarchy of relevant objects is as shown:
How it works is that whenever the player respawns, a Player.CharacterAdded
event within MeleeWeaponScript
runs and fires the UpdateName
RemoteEvent, taking several of STANDARD SWORD
's details (tool
references STANDARD SWORD
by script.Parent
) as arguments for the RemoteEvent.
player.CharacterAdded:Connect(function()
print(player) --The reference to player has been established beforehand.
print("Character Added " .. time())
player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").ShakingFrame.Weapon1Frame:WaitForChild("UpdateName"):FireClient(player, tool.Name, tool.ToolTip, tool.TextureId)
print("Fired? " .. time())
end)
Afterwards, some code within the LocalScript Weapon1Init
will check for firing of the UpdateName
RemoteEvent.
script.Parent.UpdateName.OnClientEvent:Connect(function(name, special, image)
print("UPDATE NAME " .. name .. " " .. special .. " " .. time())
script.Parent.WeaponNameText.Text = name
script.Parent.SpecialMoveText.Text = special
script.Parent.WeaponImage.Image = image
end)
When I go on testing, the weapon indicator seems to work as intended when I spawn for the first time (and unfortunately, only for the first time).
The output is as shown below (notice the time()
outputs):
squareHEADnoob
Character Added 1.2833333993331
Fired? 1.2833333993331
UPDATE NAME STANDARD SWORD [F] - LUNGE 1.3166667381302
However, if my character gets killed by any means and respawns afterwards, the weapon indicator stays as per the template (I haven’t done the proper implementation for the second weapon indicator yet):
The output still stays quite similar, but the OnClientEvent
seems to have been called before the CharacterAdded
finishes?:
squareHEADnoob
Character Added 292.01668190118
Fired? 292.01668190118
UPDATE NAME STANDARD SWORD [F] - LUNGE 291.99584858026
From Roblox’s API on PlayerGui :
“When a player first joins a game, their PlayerGui is automatically inserted into their Player
object. When the player’s Player.Character
spawns for the first time all of the contents of StarterGui
are automatically copied into the player’s PlayerGui. Note that if Players.CharacterAutoLoads
is set to false the character will not spawn and StarterGui contents will not be copied until Player:LoadCharacter
is called. If StarterGui.ResetPlayerGuiOnSpawn
is set to true then every time the player’s character respawns all of the contents of that player’s PlayerGui is cleared and replaced with the contents of StarterGui.”
I’m suspecting that the entire PlayerGui gets cloned from StarterGui upon the respawning of the character. That’s why this problem happens. What would be the workaround for this then, so that the weapon indicator updates correctly every time the character spawns?