Whats wrong with my script?

Im trying to make a game where when you kill someone you gain 10 health and when you die you lose 10 health. When you lose all your health you should be banned for 1hour.

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 kills, deaths, and health leaderboard works, but the player health doesn’t change. WHATS WRONG?!?

2 Likes

What do you mean by your health doesn’t change, do you mean that the health in the leaderboard doesn’t change?

The health in the leaderboard changes but when you get a kill/die the player health doesnt change

That’s because you haven’t made a part in the script to change the player’s actual hp

Replace the FakeHP.Changed event with this

FakeHP.Changed:Connect(function(Value)
	if Value <= 0 then
		Player.Banned.Value = true
		banPlayer(Player, 1)
    else
        local character = Player.Character or Player.CharacterAdded:Wait()
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
           humanoid.Health = FakeHP.Value       
        end
	end
end)

Could you put your part into the full script for me? Thanks. Its hard to understand where to insert your script.

Alright

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)
    else
        local character = Player.Character or Player.CharacterAdded:Wait()
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
           humanoid.Health = FakeHP.Value       
        end
	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)

Nothing works. There is no leaderboard or nothing.

The issue with the script seems to be that you’re trying to change the “Health” value of a custom “leaderstats” object you’ve created, rather than the actual Health property of the Humanoid object associated with the player’s character.

Replace and add this code where this code starts and ends

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
                    
                    -- Find the killer's character and humanoid
                    local killerCharacter = Killer.Character
                    local killerHumanoid = killerCharacter and killerCharacter:FindFirstChild("Humanoid")
                    
                    -- Only update health if the killer's humanoid was found
                    if killerHumanoid then
                        killerHumanoid.Health = killerHumanoid.Health + 10
                        HealthKL.Value = killerHumanoid.Health
                    end
                end 
            end
        end
        Deaths.Value = Deaths.Value + 1
        local playerHumanoid = Player.Character and Player.Character:FindFirstChild("Humanoid")
        if playerHumanoid then
            playerHumanoid.Health = playerHumanoid.Health - 10
            Player.leaderstats.Health.Value = playerHumanoid.Health
        end
    end)
end

Just copy the entire code? Thanks!!!

Only replace what is needed, keep alot of the original code. Please click finished solution if it works.

Can you put the complete code pls. If it works ill click it.

What do you mean there is no leaderboard?

when I play the game there is no leaderboard

I just tested the script I gave you and yes there is a leaderboard when you put in the script
image
Are you sure your putting in the script correctly? Remember to delete the original code and replace the new one.

Well can you type in the full code because I used only your code.

I already typed the full code above but I’ll put it again

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)
    else
        local character = Player.Character or Player.CharacterAdded:Wait()
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
           humanoid.Health = FakeHP.Value       
        end
	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)

When you kill someone with a gun nothing happenes. Pls fix it.

Idk where to add this code. Could you put your code into my code and post the final result