Im making a game where when you die you lose max health and when you get a kill you gain health.
Code:
local Players = game:GetService("Players")
local DEFAULT_HEALTH = 100
local MINIMAL_MAX_HEALTH = 10
local template = Instance.new('Folder') do
template.Name = 'leaderstats'
local kills = Instance.new('IntValue')
kills.Name = 'Kills'; kills.Parent = template
local deaths = Instance.new('IntValue')
deaths.Name = 'Deaths'; deaths.Parent = template
end
local playerMaxHealth = {}
Players.PlayerAdded:Connect(function(player)
local leaderstats = template:Clone()
leaderstats.Parent = player
local kills = leaderstats.Kills
local deaths = leaderstats.Deaths
local currentCharacter
playerMaxHealth[player] = DEFAULT_HEALTH
player.CharacterAdded:Connect(function(character)
if currentCharacter then currentCharacter:Destroy() end
currentCharacter = character
local humanoid = character:WaitForChild('Humanoid')
humanoid.MaxHealth = playerMaxHealth[player]
humanoid.Health = playerMaxHealth[player]
humanoid.Died:Once(function()
deaths.Value += 1
if playerMaxHealth[player] -10 < MINIMAL_MAX_HEALTH then
playerMaxHealth[player] = MINIMAL_MAX_HEALTH
else
playerMaxHealth[player] -= 10
end
for _, child in ipairs(humanoid:GetChildren()) do
if child:IsA('ObjectValue') and child.Value and child.Value:IsA("Player") then
local killer = child.Value
killer.leaderstats.Kills.Value += 1 -- definitely loaded
playerMaxHealth[killer] += 10
local killerHumanoid = killer.Character and killer.Character:FindFirstChild("Humanoid")
killerHumanoid.MaxHealth = playerMaxHealth[killer]
break;
end
end
end)
end)
end)
It ins server script service.
How would i make it so that when you lose all your health you get banned for 1 hour with a message?
You would need to save the time when they were banned and kick them if their ban time has not been reached.
I would recommend learning about data stores so you can get a good grasp on how this works.
Fixed code:
--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
--//Variables
local PlayerData = DataStoreService:GetDataStore("PlayerDataStore")
--//Tables
local sessionData = {}
--//Controls
local DEFAULT_HEALTH = 100
local MINIMAL_MAX_HEALTH = 10
--//Functions
local function Reconcile(source, template)
for k, v in pairs(template) do
if not source[k] then
source[k] = v
end
end
return source
end
local function LoadData(player)
local success, result = pcall(function()
return PlayerData:GetAsync(player.UserId)
end)
if not success then
warn(result)
end
return success, result
end
local function SaveData(player, data)
local success, result = pcall(function()
PlayerData:SetAsync(player.UserId, data)
end)
if not success then
warn(result)
end
return success
end
local function LeaderboardSetup(playerData)
local template = Instance.new("Folder")
template.Name = "leaderstats"
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Value = playerData.Kills
kills.Parent = template
local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Value = playerData.Deaths
deaths.Parent = template
return template
end
local function OnPlayerRemoving(player)
SaveData(player, sessionData[player.UserId])
end
Players.PlayerAdded:Connect(function(player)
--//Load data
local success, data = LoadData(player)
sessionData[player.UserId] = Reconcile(success and data or {}, {
Kills = 0,
Deaths = 0,
MaxHealth = 100,
LastBanned = 0,
})
--//Load leaderstats
local leaderstats = LeaderboardSetup(sessionData[player.UserId])
leaderstats.Parent = player
--//Check ban time
if os.time() - sessionData[player.UserId].LastBanned < 3600 then
player:Kick("You are still banned.")
return
end
local deaths = leaderstats.Deaths
local currentCharacter = nil
player.CharacterAdded:Connect(function(character)
if currentCharacter then currentCharacter:Destroy() end
currentCharacter = character
local maxHealth = sessionData[player.UserId].MaxHealth
local humanoid = character:WaitForChild('Humanoid')
humanoid.MaxHealth = maxHealth
humanoid.Health = maxHealth
humanoid.Died:Once(function()
sessionData[player.UserId].Deaths += 1
deaths.Value += 1
if sessionData[player.UserId].MaxHealth -10 < MINIMAL_MAX_HEALTH then
--//Make player health back to default
sessionData[player.UserId].MaxHealth[player] = 100
--//Ban player (move to wherever you want)
sessionData[player.UserId].LastBanned = os.time()
player:Kick("You have been killed.")
else
sessionData[player.UserId].MaxHealth[player] -= 10
end
for _, child in ipairs(humanoid:GetChildren()) do
if child:IsA("ObjectValue") and child.Value and child.Value:IsA("Player") then
local killer = child.Value
sessionData[killer.UserId].MaxHealth += 10
sessionData[killer.UserId].Kills += 1
killer.leaderstats.Kills.Value += 1
local killerHumanoid = killer.Character and killer.Character:FindFirstChild("Humanoid")
killerHumanoid.MaxHealth = sessionData[killer.UserId].MaxHealth
break
end
end
end)
end)
--//Reload player just incase .CharacterAdded did not fire
pcall(player.LoadCharacter, player)
end)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(function()
for i, player in ipairs(Players:GetPlayers()) do
OnPlayerRemoving(player)
end
end)
I added a saving system for your kills and deaths too, feel free to ask any questions because the system may be complicated if you don’t understand how it works.
I put your code in the server script service and the leaderboard and the health doesnt change. My code I wrote works but I just need someone to add a ban thing. THANKs