Whats wrong with my script?

Im trying to make a lifesteal game where when you kill someone you gain 10 health and when you die you lose 10 health. Also, there should be a leaderboard of your kills, death, and health. When you loose all of your health you get banned for 1 hour. Here is my code.

local Players = game.Players

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"
Instance.new('IntValue', Template).Name = "Health"

local DataStoreService = game:GetService("DataStoreService")
local BanDataStore = DataStoreService:GetDataStore("Ban")

local function kickPlayer(player)
	player:Kick("Come back in an hour after the ban") -- this contains the ban message
end

local function banPlayer(player, durationInHours)
	local banExpires = os.time() + durationInHours * 3600
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), banExpires)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	else
		kickPlayer(player)
	end
end

Players.PlayerAdded:connect(function(Player)
	Instance.new('BoolValue', Player).Name = "Banned"
	
	local success, banExpires = pcall(function()
		return BanDataStore:GetAsync(tostring(Player.UserId))
	end)
	if not success then
		warn("Error checking ban status for player " .. Player.Name .. ": " .. tostring(banExpires))
		return
	end
	local currentTime = os.time()
	if banExpires and banExpires > currentTime then
		kickPlayer(Player)
	end
	
	wait(1)
	local Stats = Template:Clone()
	Stats.Parent = Player
	local Deaths = Stats.Deaths
	
	local join = true

	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild "Humanoid"
		Stats.Health.Value = Humanoid.MaxHealth
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							local Health = Killer.leaderstats.Health
							Kills.Value = Kills.Value + 1
							Health.Value = 100

							Humanoid.MaxHealth = Kills.Value*10+100 - Deaths.Value*10

							Health.Value = Health.Value + 10
						end
						return 
					end
				end
			end)
		end
		if join then join = false return end
		Deaths.Value = Deaths.Value + 1
		
		Player.Banned.Value = true
		banPlayer(Player, 1)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), player.Banned.Value)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	end
end)

Problems: You get banned when you die 1 time.

What exactly is the issue?

1 Like

You get banned when you die 1 time instead of losing all your health

What I’m getting is you’re suppose to be banned when you die right?

No. In my game when you die you lose 10 health and when you get a kill you gain 10 health. When you lose all of your health, you shoudl get banned

But it alraedy becomes max value when the player spawns?
and died connection will fire when player health reaches 0 soo…

How would you fix it. Thanks!!!

Try this.

local Players = game:GetService("Players")



local DataStoreService = game:GetService("DataStoreService")
local BanDataStore = DataStoreService:GetDataStore("Ban")

local function kickPlayer(player)
	player:Kick("Come back in an hour after the ban") -- this contains the ban message
end

local function banPlayer(player, durationInHours)
	local banExpires = os.time() + durationInHours * 3600
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), banExpires)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	else
		kickPlayer(player)
	end
end

Players.PlayerAdded:connect(function(Player)
	local LD = Instance.new("Folder")
	LD.Name = "leaderstats"
	LD.Parent = Player
	local success, banExpires = pcall(function()
		return BanDataStore:GetAsync(tostring(Player.UserId))
	end)
	if not success then
		warn("Error checking ban status for player " .. Player.Name .. ": " .. tostring(banExpires))
		return
	end
	local currentTime = os.time()
	if banExpires and banExpires > currentTime then
		kickPlayer(Player)
	end

	wait(1)
	local FakeHP = Instance.new("IntValue")
	FakeHP.Value = 100
	FakeHP.Name = "Health"
	FakeHP.Parent = LD
	local Deaths = Instance.new("IntValue")
	Deaths.Value = 0
	Deaths.Name = "Deaths"
	Deaths.Parent = LD
	local Kills = Instance.new("IntValue")
	Kills.Value = 0
	Kills.Name = "Kills"
	Kills.Parent = LD
	local join = true
	FakeHP.Changed:Connect(function(Value)
		if Value <= 0 then
			Player.Banned.Value = true
			banPlayer(Player, 1)
		end
	end)
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local KillsKL = Killer.leaderstats.Kills
							local HealthKL = Killer.leaderstats.Health
							KillsKL.Value = KillsKL.Value + 1

							Humanoid.MaxHealth = KillsKL.Value*10+100 - Deaths.Value*10

							HealthKL.Value = HealthKL.Value + 10
						end 
					end
				end
				Deaths.Value = Deaths.Value + 1
				Player.leaderstats.Health.Value -= 10
			end)
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), player.Banned.Value)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	end
end)

The leaderboard doesnt work, and you dont gain/lose health. Your script has some buggs.

Hello, If I get your problem right, the issue with your script is that you’re banning the player every time they die. This is due to the line ban Player (Player, 1) being called each time a player’s character dies.
In your requirements, you mentioned that a player should be banned only when they lose all their health, but your code currently bans them regardless of their remaining health. What you need to do is check whether the player’s health after dying is less than or equal to zero before banning them.

You could accomplish this by doing something like this:

Player.CharacterAdded:connect(function(Character)
    local Humanoid = Character:FindFirstChild "Humanoid"
    Stats.Health.Value = Humanoid.MaxHealth
    if Humanoid then
        Humanoid.Died:connect(function()
            for i, Child in pairs(Humanoid:GetChildren()) do
                if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
                    local Killer = Child.Value
                    if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
                        local Kills = Killer.leaderstats.Kills
                        local Health = Killer.leaderstats.Health
                        Kills.Value = Kills.Value + 1
                        Health.Value = 100

                        Humanoid.MaxHealth = Kills.Value*10+100 - Deaths.Value*10

                        Health.Value = Health.Value + 10
                    end
                    return 
                end
            end
        end)
    end
    if join then join = false return end
    Deaths.Value = Deaths.Value + 1

    if Stats.Health.Value <= 0 then
        Player.Banned.Value = true
        banPlayer(Player, 1)
    end
end)

This code will check if the player’s health is less than or equal to zero after they die, and only then will it ban the player. It does this by checking the value of Stats.Health.Value after incrementing the Deaths.Value . If the health is less than or equal to zero, the player is banned. Let me know If It helped or not!

Ok Ill test it out. THANKS!!!

A lot of your code has errors. Could you check it again.

Everything works but your health doesnt change.

for killer or not?

For both killer and deather. The leaderboard is fine. IDK whats wrong

I looked through the entire script and cant find why the health doesnt change.

not sure but maybe because the script is changing the max health of the humanoid before they respawn or the max health doesn’t save

Could someone pls change the code to actually change the health? And explain whats wrong. Thanks

local Players = game:GetService("Players")



local DataStoreService = game:GetService("DataStoreService")
local BanDataStore = DataStoreService:GetDataStore("Ban")

local function kickPlayer(player)
	player:Kick("Come back in an hour after the ban") -- this contains the ban message
end

local function banPlayer(player, durationInHours)
	local banExpires = os.time() + durationInHours * 3600
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), banExpires)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	else
		kickPlayer(player)
	end
end

Players.PlayerAdded:connect(function(Player)
	local LD = Instance.new("Folder")
	LD.Name = "leaderstats"
	LD.Parent = Player
	local success, banExpires = pcall(function()
		return BanDataStore:GetAsync(tostring(Player.UserId))
	end)
	if not success then
		warn("Error checking ban status for player " .. Player.Name .. ": " .. tostring(banExpires))
		return
	end
	local currentTime = os.time()
	if banExpires and banExpires > currentTime then
		kickPlayer(Player)
	end

	wait(1)
	local FakeHP = Instance.new("IntValue")
	FakeHP.Value = 100
	FakeHP.Name = "Health"
	FakeHP.Parent = LD
	local Deaths = Instance.new("IntValue")
	Deaths.Value = 0
	Deaths.Name = "Deaths"
	Deaths.Parent = LD
	local Kills = Instance.new("IntValue")
	Kills.Value = 0
	Kills.Name = "Kills"
	Kills.Parent = LD
	local join = true
	FakeHP.Changed:Connect(function(Value)
		if Value <= 0 then
			Player.Banned.Value = true
			banPlayer(Player, 1)
		end
	end)
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local KillsKL = Killer.leaderstats.Kills
							local HealthKL = Killer.leaderstats.Health
							KillsKL.Value = KillsKL.Value + 1

							Humanoid.MaxHealth = KillsKL.Value*10+100 - Deaths.Value*10
							
							HealthKL.Value = HealthKL.Value + 10
						end 
					end
				end
				Deaths.Value = Deaths.Value + 1
				Player.leaderstats.Health.Value -= 10
			end)
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), player.Banned.Value)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	end
end)

I never actually see the Humanoids.Health property being set. Try this?

local Players = game:GetService("Players")



local DataStoreService = game:GetService("DataStoreService")
local BanDataStore = DataStoreService:GetDataStore("Ban")

local function kickPlayer(player)
	player:Kick("Come back in an hour after the ban") -- this contains the ban message
end

local function banPlayer(player, durationInHours)
	local banExpires = os.time() + durationInHours * 3600
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), banExpires)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	else
		kickPlayer(player)
	end
end

Players.PlayerAdded:connect(function(Player)
	local LD = Instance.new("Folder")
	LD.Name = "leaderstats"
	LD.Parent = Player
	local success, banExpires = pcall(function()
		return BanDataStore:GetAsync(tostring(Player.UserId))
	end)
	if not success then
		warn("Error checking ban status for player " .. Player.Name .. ": " .. tostring(banExpires))
		return
	end
	local currentTime = os.time()
	if banExpires and banExpires > currentTime then
		kickPlayer(Player)
	end

	wait(1)
	local FakeHP = Instance.new("IntValue")
	FakeHP.Value = 100
	FakeHP.Name = "Health"
	FakeHP.Parent = LD
	local Deaths = Instance.new("IntValue")
	Deaths.Value = 0
	Deaths.Name = "Deaths"
	Deaths.Parent = LD
	local Kills = Instance.new("IntValue")
	Kills.Value = 0
	Kills.Name = "Kills"
	Kills.Parent = LD
	local join = true
	FakeHP.Changed:Connect(function(Value)
		if Value <= 0 then
			Player.Banned.Value = true
			banPlayer(Player, 1)
		end
	end)
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local KillsKL = Killer.leaderstats.Kills
							local HealthKL = Killer.leaderstats.Health
							KillsKL.Value = KillsKL.Value + 1

							Humanoid.MaxHealth = KillsKL.Value*10+100 - Deaths.Value*10
                                                        Humanoid.Health = Humanoid.MaxHealth -- code added here
							HealthKL.Value = HealthKL.Value + 10
						end 
					end
				end
				Deaths.Value = Deaths.Value + 1
				Player.leaderstats.Health.Value -= 10
			end)
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local success, result = pcall(function()
		BanDataStore:SetAsync(tostring(player.UserId), player.Banned.Value)
	end)
	if not success then
		warn("Error saving ban status for player " .. player.Name .. ": " .. tostring(result))
	end
end)

The problem here is when your character respawns after you join, you get banned. You need a condition that checks if your health depletes to zero, which is something you did not check.