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

XP on Hit/Kill NPC Not working? No errors?

Asked by
BryanFehr 133
5 years ago
Edited 5 years ago

So I've been trying to get my XP Script to function so if the player's bullet hits the NPC, you gain a certain number of XP, and once you kill that NPC, you gain a certain number as well, yet all my efforts on trying to get this solved haven't worked. Yet, there's no errors in the script, or when ran. So, if anyone knows how to fix this script or what to change, I'd love to the help! Thanks!

function onHealthChanged() 
    local zHuman =  script.Parent:findFirstChild( "Humanoid" )
    if zHuman ~= nil then 
        local tag = zHuman:findFirstChild( "creator" ) 
        if tag ~= nil then 
            if tag.Value ~= nil then 
                local stats = tag.Value:findFirstChild("XP") 
                if stats ~= nil then 
                    if tag.Value.Character ~= nil then 
                        stats.XP.Value = stats.XP.Value + 20 
                    end
                end
            end 
        end 
    end

end 

function onDeath()
    local zHuman =  script.Parent:findFirstChild( "Humanoid" )
    if zHuman ~= nil then 
        local tag = zHuman:findFirstChild( "creator" ) 
        if tag ~= nil then 
            if tag.Value ~= nil then 
                local stats = tag.Value:findFirstChild("XP") 
                if stats ~= nil then 
                    if tag.Value.Character ~= nil then 
                        stats.XP.Value = stats.XP.Value + 200 
                    end
                end
            end 
        end 
    end

end 
script.Parent.Humanoid.Died:connect(onDeath) 
script.Parent.Humanoid.HealthChanged:connect(onHealthChanged) 
0
Is this script in the player's character? Miniller 562 — 5y
0
Script is in the NPC's character. BryanFehr 133 — 5y
0
A lot of thing are deprecated in this script. E.g 'findFirstChild" should be 'FindFirstChild'. and at the end, 'connect' should be 'Connect' Miniller 562 — 5y
0
here's a fact: leaderstats are not located in the character with the Humanoid, that's why it isn't working DeceptiveCaster 3761 — 5y
View all comments (3 more)
0
Okay, so where should I place the script? In ServerScriptService, Workspace, or? BryanFehr 133 — 5y
0
inside of the player DeceptiveCaster 3761 — 5y
0
but in order to make that work you have to edit the script quite a bit: for example, find when the bullet hits the NPC from a server script, use a remote event to fire that event to the client and then change the stats via the client DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Okay well when you're doing the if else statement on line 3 you are not calling the health you are just calling the property. If it were nill then it would not exist. And it always exist. To fix it just do "Zhuman.health"

Ad

Answer this question