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?!?
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)
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)
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
I just tested the script I gave you and yes there is a leaderboard when you put in the script
Are you sure your putting in the script correctly? Remember to delete the original code and replace the new one.
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)