Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to give EXP when Mob Dies in whenever its HealthChanged?

Asked by 3 years ago

I know that there is a humanoid.Dies event and it can connect me to the player who killed it. But I want to find the player who kills the mob when the humanoid.Healthchanged event. In the code, "if currentHealth == 0" I want to figure out who the player is that made its health to 0.

As for giving exp to the player, I got that sorted it out. What I really need is, how to find the player's name who killed this mob.

humanoid.HealthChanged:Connect(function(newHealth)

    local healthDifference = newHealth - humCurrentHealth
    humCurrentHealth = newHealth

    currentHealth = math.clamp(currentHealth + healthDifference, 0, maxHealth)

    bar.Size = UDim2.new(1/maxHealth*currentHealth, 0, 1, 0)
    healthTxt.Text = currentHealth .. "/" .. maxHealth


    if currentHealth == 0 then

        mob.Parent = game.ServerStorage

        currentHealth = maxHealth
        bar.Size = UDim2.new(1/maxHealth*currentHealth, 0, 1, 0)
        healthTxt.Text = currentHealth .. "/" .. maxHealth

        humanoid.Health = humanoid.MaxHealth


        humanoid:MoveTo(Vector3.new(0, 0, 0))
        mob.HumanoidRootPart.CFrame = spawnCFrame

        print("died")
        wait(respawnTime)

        mob.Parent = workspace


    end
end)

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

it's not that hard to do, there is a hacky way that roblox uses in their models to check for kills and who to give the kill to. It's a creator value, which is an object value that would store the player (the player, not the name because it's an objectValue). What you could do is tag the mob that gets hit by your weapon inside of your weapons

-- has to be in all your weapons, unless you don't want it to give xp

-- When you check hit detection
if (hit.Parent:FindFirstChild("Humanoid")) then
    local creator = Instance.new("ObjectValue")
    creator.Name = "creator"
    creator.Value = player -- Your player
    creator.Parent = hit.Parent:WaitForChild("Humanoid")
    game:GetService("Debris"):AddItem(creator, 2) -- Remove it after 2 seconds, unless you want only the first person to get xp, you probably have to create your own system if you want to give each player the proportion of xp to the amount of damage they did
end

then in the mob script, you can check if a creator value exists and get the player who killed the mob

if (currentHealth == 0)
    local player
    if (humanoid:FindFirstChild("creator") and humanoid.Creator.Value) then
        player = humanoid.Creator.Value
    end

    if (player) then
        -- Give xp here
    end
    mob.Parent = game.ServerStorage 
    currentHealth = maxHealth
    bar.Size = UDim2.new(1/maxHealth*currentHealth, 0, 1, 0)
    healthTxt.Text = currentHealth .. "/" .. maxHealth

    humanoid.Health = humanoid.MaxHealth

    humanoid:MoveTo(Vector3.new(0, 0, 0))
    mob.HumanoidRootPart.CFrame = spawnCFrame

    print("died")
    wait(respawnTime)    
    mob.Parent = workspace
end
0
Thank you AnasBahauddin, you taught me more uses for creator.value than I know TicTocHat 6 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

if you look in the classic sword model, you will see how they tag the player when they dmg and un-tags the last player that attacked if there is one. You could do something like that in your dmg script.

Answer this question