Make A Server Script Run again after Player has died

I have a serverscript that checks the player for certain badges.

But the serverscript does not run again after the player respawns after death.

This post is short because this is already full with context.

Can Anyone Help me with this?

Thanks for your time.

1 Like

There are couple of ways to solve this.

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.

It runs under a PlayerAdded event, but would it stop working if I change it to CharacterAdded?

Don’t change the PlayerAdded to CharacterAdded,
But add CharacterAdded event inside the PlayerAdded event

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

How would that work here exactly?

game:GetService("Players").PlayerAdded:Connect(function(player)

I dont know how I would use a Character added on a parameter

It would be something like this:

local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(player)
       Players.CharacterAdded:Connect(function(character)


   end)
end)

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)

no script errors

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

Sorry If I haven’t been on the Devforum lately, but your solution is correct