One of them, is adding CharacterAdded event(if its a PlayerAdded event).
And what’d happen with that is,every time the character appears in-game, the script will run for that player.
If its not under a PlayerAdded , you could either make a loop or a function that checks whenever the player respawns - meaning, his character loaded again.
You can try what @Valkyrop suggested or place your server script(assuming it’s a player related script not something like a game loop) inside StarterPlayer.StarterCharacterScripts this will make the script run every time the character respawns and remove any unnecessary code such as PlayerAdded and CharacterAdded. To reference the specific player running the script you have to do:
local Character = script.Parent --since the script runs inside their character
local Player = game.Players:GetPlayerFromCharacter(Character)
local Humanoid = Character:WaitForChild("Humanoid")
print(Player, Character, Humanoid) --prints everytime the player respawns
CharacterAdded is an event of the player instance(the player parameter), therefore:
local Players = game:GetService("Players")
function CharacterAdded(character)
local player = Players:GetPlayerFromCharacter(character)
--code which runs on player respawn
end
Players.PlayerAdded:Connect(function(player)
--in case their character loads before the script
CharacterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(CharacterAdded)
end)
Tried using ur script, but it still stops working on respawn.
this is the script:
local BadgeService = game:GetService("BadgeService")
local BadgeId = 123456789
local Players = game:GetService("Players")
function CharacterAdded(character)
local player = Players:GetPlayerFromCharacter(character)
if BadgeService:UserHasBadge(player.UserId, BadgeId) then
print("The user has this badge")
else
print("The user does not have this badge")
script.ScreenGui.Parent = player.PlayerGui
end
end
Players.PlayerAdded:Connect(function(player)
CharacterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(CharacterAdded)
end)
Move your script to game.StarterPlayer.StarterCharacterScripts instead:
--parent the script inside game.StarterPlayer.StarterCharacterScripts
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BadgeId = 123456789
local character = script.Parent
local player = Players:GetPlayerFromCharacter(character)
--use UserHasBadge if it's a LocalScript
if BadgeService:UserHasBadgeAsync(player.UserId, BadgeId) then
print("The user has this badge")
else
print("The user doesn't have this badge")
script.ScreenGui.Parent = player.PlayerGui
end