Lives system that kicks players when their lives are gone

Hey! So im trying to make a lives system where you have 3 lives, when you die 3 times you get kicked. But I just can’t really do it.

This is my attempt:

--[[ Variables ]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local char = player.Character
	local hum = char.Humanoid
	local screen = player:WaitForChild("PlayerGui").Screen
	
	local lives = Instance.new("IntValue")
	lives.Value = 3
	lives.Parent = game.Workspace
	
	--[[ Death ]]--
	hum.Died:Connect(function()
		
		lives.Value = lives.Value - 1
		
		if lives == 2 then
			screen.BG.Life3.ImageColor3 = Color3.fromRGB(0,0,0)
		elseif lives == 1 then
			screen.BG.Life3.ImageColor3 = Color3.fromRGB(0,0,0)
			screen.BG.Life2.ImageColor2 = Color3.fromRGB(0,0,0)
		elseif lives == 0 then
			screen.BG.Life3.ImageColor3 = Color3.fromRGB(0,0,0)
			screen.BG.Life2.ImageColor2 = Color3.fromRGB(0,0,0)
			screen.BG.Life1.ImageColor1 = Color3.fromRGB(0,0,0)
			wait(5)
			player:Kick("Only the strong survive, try be careful next time.")
		end
		
		if lives > 3 then
			player:Kick("You're so health! Too healthy...")
		end 
		
	end)
end)

Can anyone please help?

EDIT: I also keep getting the error: Screen is not a valid member of PlayerGui “Players.ROBLOX_Test00.PlayerGui”

1 Like

I will get back to you, I am making a script right now.

1 Like

Also make it a part of leaderstats, a value being in Workspace simply wont work.

2 Likes

This is happening because of the fact that you only connect the Humanoid.Died event when the player first joins, but you NEVER set it again after that, so the amount of lives they have would only ever go down to 2.

Try using this instead:


Players.PlayerAdded:Connect(function(player)
	local screen = player:WaitForChild("PlayerGui").Screen
	
	local lives = Instance.new("IntValue")
	lives.Value = 3
	lives.Parent = game.Workspace

        Player.CharacterAdded:Connect(function(char)
		local hum = char.Humanoid
	
		--[[ Death ]]--
		hum.Died:Connect(function()
		
			lives.Value = lives.Value - 1
		
			if lives == 2 then
				screen.BG.Life3.ImageColor3 = Color3.fromRGB(0,0,0)
			elseif lives == 1 then
				screen.BG.Life3.ImageColor3 = Color3.fromRGB(0,0,0)
				screen.BG.Life2.ImageColor2 = Color3.fromRGB(0,0,0)
			elseif lives == 0 then
				screen.BG.Life3.ImageColor3 = Color3.fromRGB(0,0,0)
				screen.BG.Life2.ImageColor2 = Color3.fromRGB(0,0,0)
				screen.BG.Life1.ImageColor1 = Color3.fromRGB(0,0,0)
				wait(5)
				player:Kick("Only the strong survive, try be careful next time.")
			end
		
			if lives > 3 then
				player:Kick("You're so health! Too healthy...")
			end 
		
		end)
	end)
end)

I hope this helps!

1 Like

I tried this, but it didn’t work.

Going off of the edit that you added onto the original post, it’s likely because Screen hasn’t replicated yet.

Replace this line of code:

local screen = player:WaitForChild("PlayerGui").Screen

with this:

local screen = player:WaitForChild("PlayerGui"):WaitForChild("Screen") -- Make sure to wait until the object is added.

I hope this helps!

1 Like

Make the parent for the IntValue the localplayer. Workspace is serversided so it wouldnt work and would kick all. You should do…

local lives = Instance.new("IntValue")
	lives.Value = 3
	lives.Parent = game.Players.LocalPlayer

which makes it local and easier to handle.

hope this helps.

1 Like

This fixed the error! Thank you!

Unfortuantly, im using a server sided script, so this would not work.

I don’t think you should trust the client to kick itself.

How?

Its for everyone
Anything that happens there is for the server
UNLESS
its CREATED locally.

Yes, I still do not get how can kicking one person from serverside kick everyone.