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

I can't get this npc to give the player strength when its killed. Fix?

Asked by 3 years ago

The player presses Q to punch and i want to make it so when you kill a npc you get more strength. For the 1st npc i want it to give you 10k strength per kill.

This is the script i have inside the npc, its not adding the strength when the npc dies. Theres no errors in output either.

I've tried making it so when the npc dies instead of the reward being 10k strength i put print "success" just to see if anything printed but it didn't so I'm thinking maybe the game isn't detecting that the npc died so I'm not getting the reward? I have no idea

local Humanoid = script.Parent;
local AmountToGive = 10000

Humanoid.Died:Connect(function() 
    local tagValue = Humanoid:FindFirstChild("creator").Value
    if tagValue then
        local leaderstats = tagValue:FindFirstChild("leaderstats")
        leaderstats.strength.Value += AmountToGive
        wait(0.1)
    end
    script:Destroy()
end)

1 answer

Log in to vote
0
Answered by 3 years ago

This is kinda inefficient but could work.

local Humanoid = script.Parent;
local AmountToGive = 10000
while true do 
    if Humanoid.Health == 0 or Humanoid.Health < 1 or Humanoid.Health < 0  then
        local tagValue = Humanoid:FindFirstChild("creator").Value
        if tagValue ~= nil then
            local leaderstats = tagValue:FindFirstChild("leaderstats")
            leaderstats.strength.Value += AmountToGive
            wait(0.1)
        end
        script:Destroy()
        wait()
    end
end

If this doesn't work it's probably because the "tagValue" you're looking for is nil and you'll need to create a value or find a way the player did damage to the NPC.

0
i got the error - Script timeout: exhausted allowed execution time ItzSulfonic 61 — 3y
0
Probably due to the fact I'm using while true do. Just get rid of while true do and have it detect when the health is changed and when it hits 0 or lower then 0. You can find on google how to detect when a humanoid's Health changes. DogCooper 2 — 3y
Ad

Answer this question