LocalPlayerScripts - Script doesn't work after I die

Hey !

I’m here again on this script that displays the name of the player when you hover him. This time, a friend made one, it works very well. He told me to put it into LocalPlayerScripts but forgot to tell me that the script stops after you die. Could you help me about that annoying issue ? How can I modify the script to make it work after a death without joining the server back ?

local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local mouse = player:GetMouse()

local screenGui = Instance.new("ScreenGui", playerGui)
local mouseLabel = Instance.new("TextLabel", screenGui)

mouseLabel.BackgroundTransparency = 1
mouseLabel.Size = UDim2.new(0, 200, 0, 25)
mouseLabel.Font = Enum.Font.SourceSans
mouseLabel.Text = ""
mouseLabel.TextScaled = true
mouseLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
mouseLabel.TextStrokeTransparency = 0
mouseLabel.BorderSizePixel = 0
mouseLabel.TextScaled = true
mouseLabel.Visible = false

local function onMouseMove()
    local target = mouse.Target
    if target ~= nil then
        if players:GetPlayerFromCharacter(target.Parent) then
            mouseLabel.Text = target.Parent.Name
            mouseLabel.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
            mouseLabel.Visible = true
        else
            mouseLabel.Visible = false
        end
    else
        mouseLabel.Visible = false
    end
end

mouse.Move:connect(onMouseMove)

Here is the configuration of the script :

image

Thanks for helping !

StarterPlayerScripts only inserts the children into the player when they join the game, you need to reinstantiate the objects that get removed after the death of your character. To do so, listen for the adding of a new character, and instantiate them as you did previously:

local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local mouse = player:GetMouse()

repeat wait() until Player.Character

local screenGui, mouseLabel
local setup = (function ()
	screenGui = Instance.new("ScreenGui", playerGui)
	mouseLabel = Instance.new("TextLabel", screenGui)

	mouseLabel.BackgroundTransparency = 1
	mouseLabel.Size = UDim2.new(0, 200, 0, 25)
	mouseLabel.Font = Enum.Font.SourceSans
	mouseLabel.Text = ""
	mouseLabel.TextScaled = true
	mouseLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
	mouseLabel.TextStrokeTransparency = 0
	mouseLabel.BorderSizePixel = 0
	mouseLabel.TextScaled = true
	mouseLabel.Visible = false
end)

Player.CharacterAdded:connect(function ()
	setup()
end) do
	setup()
end

local function onMouseMove()
	if not screenGui or not mouseLabel then
		return
	end
    local target = mouse.Target
    if target ~= nil then
        if players:GetPlayerFromCharacter(target.Parent) then
            mouseLabel.Text = target.Parent.Name
            mouseLabel.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
            mouseLabel.Visible = true
        else
            mouseLabel.Visible = false
        end
    else
        mouseLabel.Visible = false
    end
end

mouse.Move:connect(onMouseMove)
1 Like

Thank you for your helpful and quick reply ! I’m not a scripter at all at base, I try to understand things. More into meshes, animations, textures…

Did it work?
[30 charssssssssssss]

Just saw something, and that code wouldn’t work too, you used connect(Connect with C lower case), but it is deprecated, you should use Connect(Connect with C upper case) instead.

1 Like

Although deprecated, it still works. I prefer :connect as imo it’s more attractive and saves me pressing the shift key - efficiency. It’s been deprecated since before 2010 and shows no sign of disappearing, so I refuse to change until I need to.

I modified the script a bit, you tipped Player instead of player. Thank you for the answer though !

I think the problem here is that the ScreenGui you created has ResetOnSpawn set to true, so you no longer see the same ScreenGui when you respawn. The solution is simple, just put this after you created the ScreenGui:

screenGui.ResetOnSpawn = false

image

it was on true, that’s the thing that made me thing the problem was from the script itself