So i was working on a respawn system that gives you IFrames and a Highlight everytime you respawn. The problem is that it only fires once. I,ve tried creating a new experience and do the basic code there and it still doesnt work.
game clip :
(Everytime you spawn you should have the highlight)
This is another clip of me doing it on the test zone.
Code :
--// Server
local Test_Event = game.ReplicatedStorage.Events.Testing
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Test_Event:FireAllClients("Whatever")
print("Fired event to all clients?")
end)
end)
--// Client
local Test_Event = game.ReplicatedStorage.Events.Testing
Test_Event.OnClientEvent:Connect(function(Message)
print(Message)
end)
I’m assuming your local script is in StarterCharacterScripts. The scripts inside of it seem to load a bit late because of the player’s character taking some time to load or to spawn, so I’m guessing that is the problem.
I did the exact same test as yours, one with the local script in StarterCharacterScripts which gave me the same problem you described and the other one in StarterPlayerScripts which worked fine to me.
I’ve had some problems similar to this when the scripts weren’t catching CharacterAdded events because the scripts were supossed to load along with the character and they would miss these types of events (specially in studio) , so now i just avoid putting scripts inside of StarterCharacterScripts and i put them in StarterPlayerScripts instead.
I’m just gonna elaborate on what @Nube762 said because he’s correct
On the first spawn, the client can connect before the server fires the event so you see the message print. On subsequent spawns however, the timing for the client to connect before the server fires is tighter thus the client was not able to connect before the server fired the event and you didn’t see the message print.
This connect/not connect thing only exists because the script was placed in character scripts, where a new script was cloned and the old one was destroyed. The time frame between creation and destruction is likely when the server fires the event thus no script picked it up. Placing it in starter player, therefore, works because the script is never destroyed to miss an event fire by the server