Problems with life remaining script

Hi, I’m trying to make 5 lives left after you die and when you hit 0 it kicks you but my scripting didn’t work
and I don’t know what’s wrong.
Script:

local l = 5

local function death(char)
	script.Parent.Parent.Enabled = true
	l = l-1
	local player = game.Players:GetPlayerFromCharacter(char)
	script.Parent.Text = (l .. " LIVES REMAIN")
	if l == 1  then
		script.Parent.Text = (l .. "LIFE REMAINS")
	elseif l == 0 then
		player:Kick("You have ran out of lives.")
	end
	wait(3)
	script.Parent.Parent.Enabled = false
end
local function new(player)
	player.CharacterRemoving:Connect(death)
end

game.Players.PlayerAdded:Connect(new)

It parented to a TextLabel inside GUI
When you die it kicks you even if you have 5 lives.
When you die it doesn’t show you the GUI.

New Info: New info: it doesn’t run the death function.

Try creating a debounce or use hum.Died

Can you put a print inside the death() function?

print(l)

Could be because the Gui resets on spawn. Find the ScreenGui it’s in, and turn off ResetOnSpawn for it, and see if it shows.

Didn’t work still I think it might be because of local and server

ServerSided Code
local Players = game:GetService("Players")

local Parent = script.Parent

function PlayerAdded(Player)
    local Lives = 5

    Parent.Text = "5 LIVES REMAIN"

    Player.CharacterAdded:Connect(function(Character)
        Character:WaitForChild("Humanoid").Died:Connect(function()
            Lives -= 1

            Parent.Parent.Enabled = true
            Parent.Text = Lives..(if Lives == 1 then "LIFE REMAINS" else "LIVES REMAIN")

            if Lives == 0 then
                Player:Kick("You have ran out of lives.")
            end

            task.wait(3)

            Parent.Parent.Enabled = false
        end)
    end)
end

Players.PlayerAdded:Connect(PlayerAdded)

for _, Player in ipairs(Players:GetPlayers()) do
    PlayerAdded(Player)
end

couple of things to keep in mind here
this will kick the player after 5 lives, but the Enabled and Text properties will be server sided if this is a server script

simply use a local script instead

LocalSided Code
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Parent = script.Parent

local Lives = 5

Parent.Text = "5 LIVES REMAIN"

Player.CharacterAdded:Connect(function(Character)
    Character:WaitForChild("Humanoid").Died:Connect(function()
        Lives -= 1

        Parent.Parent.Enabled = true
        Parent.Text = Lives..(if Lives == 1 then "LIFE REMAINS" else "LIVES REMAIN")

        if Lives == 0 then
            Player:Kick("You have ran out of lives.")
        end

        task.wait(3)

        Parent.Parent.Enabled = false
    end)
end)

the local script will work with multiple people in game

2 Likes
local lives = 5
local screenGui = script.Parent.Parent
local label = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	screenGui.Enabled = true
	lives -= 1
	if lives >= 2 then
		label.Text = (lives.." lives remaining!")
	elseif lives == 1 then
		label.Text = (lives.." life remaining!")
	elseif lives == 0 then
		label.Text = (lives.." lives remaining!")
		task.wait(3)
		player:Kick("You have ran out of lives.")
	end
	screenGui.Enabled = false
end)

The player’s Gui should only ever be handled on the client-side through the use of local scripts.

I made two separate scripts for a reason

It was a message of clarification for the thread’s poster.

1 Like

oh my bad then, thought it was towards me because you replied to me