My RemoteEvent only fires once after CharacterAdded() is called

the title says it all. Basically, I have a script in ServerScriptService that fires a remote event everytime a player’s character loads. The problem is that it works once (after the player has joined) but not when the player resets or dies. How do I fix this? Here’s some of the code:

ServerScriptService:

game.Players.PlayerAdded:Connect(function(plr : Player)
    plr.CharacterAdded:Connect(function(character : Model)	
	   game.ReplicatedStorage.Events.InitShop:FireClient(plr)
    end)
end)

Local Script in StarterGUI:

game.ReplicatedStorage.Events.InitShop.OnClientEvent:Connect(function()
	for i, v in pairs(script.Parent.Background.Content.Tables:GetChildren()) do
		for x, y in pairs(game.Players.LocalPlayer.boughttables:GetChildren()) do
			if y.Name == v.Name then
				print("equip "..v.Name)
			end
		end
	end
end)

It outputs all the “equip table” that i printed in the local script but only once. If I die or reset, i does’nt print it anymore…

Thanks!

Test with this code for when a player reset or die:

LocalScript usage:

local plr = game:GetService("Players").LocalPlayer
local character = plr.Character
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	game.ReplicatedStorage.Events.InitShop:FireServer()
end)

ServerScript usage:

local function onPlayerDied(plr)
    game.ReplicatedStorage.Events.InitShop:FireClient(plr)
end

local function onCharacterAdded(character)
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        humanoid.Died:Connect(function()
            onPlayerDied(game.Players:GetPlayerFromCharacter(character))
        end)
    end
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
    if player.Character then
        onCharacterAdded(player.Character)
    end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

It seems that I was able to reproduce this issue. I believe that after the player dies and :LoadCharacter() is called, everything in the PlayerGui will get deleted and replaced.

While the local script was being replaced, the server fired the remote event before the client reconnected the event in the new local script.

I suggest putting your client script in StarterPlayerScripts to prevent this from happening.

Another way to avoid this issue is to place the local script in the GUI itself and disable the property called “ResetOnSpawn

1 Like

Oh he speaking about the ui? So ye doesn’t need to fire to remote.

1 Like

I overlooked that part of the OP but yes it seems that he probably doesn’t need a remote event to refresh his UI because the UI already resets when the player spawns

1 Like

alright, thank you so much! I’ll go try it out.

EDIT: It seems to work, thanks a lot since this was delaying my project…