Help with my script

Im trying to make a game where when you die you lose 10 health but when you kill someone you gain 10 health.

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
local health = 100

-- Constants
local KILL_HEALTH_GAIN = 10
local DEATH_HEALTH_LOSS = 10

-- Function to handle a player getting a kill
local function onKill()
    health = health + KILL_HEALTH_GAIN
    print("You gained 10 health!")
    print("Current health:", health)
end

-- Function to handle a player's death
local function onDeath()
    health = health - DEATH_HEALTH_LOSS
    print("You lost 10 health!")
    print("Current health:", health)
end

-- Connect the functions to respective events
humanoid.Died:Connect(onDeath)
humanoid.HealthChanged:Connect(function(newHealth)
    if newHealth > health then
        onKill()
    end
end)

Whats wrong with my code.

3 Likes

The death function should work, however you have it so whenever your health changes and gets larger, it counts as if you killed someone. You need to make a script to find the killer and add to their health.

1 Like

Hmm, that isn’t obvious at first glance. I’ve got 2 comments by the way:

  1. Try to print()-debug your code and check whether everything is being called and figure out a direct problem. “What’s wrong with my code” is kinda not the best question.
  2. I don’t 100% sure but health is a variable and you better reference humanoid.Health when doing health = health + KILL_HEALTH_GAIN (humanoid.Health += KILL_HEALTH_GAIN)

Make it so when the player kills another player, it detects it by subtracting damage from the other player’s current health and seeing if it is 0 or more. Then fire the onKill() function.

For the onDeath() function, just use the humanoid.Died() event.

So first of all your script is client sided, which means the values you change are only happening for the players client, you should use a server script. Second you’re not reconnecting the humanoid functions meaning when the player dies the script will not function anymore. I recommend making functions for each event and connect them every time your player respawns. And third you should modify .MaxHealth as only changing .Health will allow the player to regen health naturally over the limit

Could you write the script? thx

1 Like

can’t write an entire script since I’m on mobile but i can write somewhat of a start

--Parent this to like server sctipt service
game.Players.PlayerAdded:Connect(function(player)
    local max=100
    --your previous functions but with "local" infront 
    player.CharacterAdded:Connect(function(character)
        local hum=character:WaitForChild("Humanoid")
        hum.MaxHealth=max
        hum.Died:Connect(died)
        hum.HealthChanged:Connect(healthChange)
    end)
end)

This stuff is so hard to work with because there is a health function always running, and it messes with anything you try to set as far as health. (Workspace.PlayerName.Health)

Also if you want health to be max health then just make some minor changes from my script using AMisspelledUsernaem’s response as an example.