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”
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)