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

why wont this function work for killing an npc?

Asked by 6 years ago

I have a script located in my StarterGui and this function in the script is not telling if the npc humanoid is at 0.

local player = script.Parent.Parent
local leaderboard = player:WaitForChild("leaderstats")
local currency1 = leaderboard:WaitForChild("Gold")

function onDied()
    print("Dead")
    currency1.Value = currency1.Value + 100
end

script.Parent.Parent.Workspace.TutPrac.Viking.Humanoid.Died:connect(onDied)


thanks!

2 answers

Log in to vote
0
Answered by
RjsMc 48
6 years ago

Its usually better to put the localscript into the Tool of the sword.

Try using a sword and use this script. (Put it into the tool)

Use this link to learn how to make a sword if you dont know. (Also needed for the "Hitmark" part in the script: How to make a sword

local hitmark = script.Parent.hitname --Let this be the part that damages the player
--(May need to watch the tutorial provided above)

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")

hitmark.Touch:connect(function(npc)
    if npc.Parent.Name == "Viking" and npc.Parent.Humanoid.Health == 0 then -- Checks if the NPC killed is named Viking and checks if dead
        leaderstats.Currency.Value = leaderstats.Currency.Value + 50 -- Adds currency
    end
end)

Please note: This has worked for me. How ever, Im not that professional at scripting but this is what I know. Use the Output of any errors. If the output does not help however, it may give you an idea on what to do.

Ad
Log in to vote
-1
Answered by 6 years ago

Well, for starters, I dont think that .Died is a function, also, there is not many places you can make the player variable script.Parent.Parent, so before we start, put this script in starter gui. that being said, I dont see anything wrong with your script other than that, so let me show you how you would do that.

local player = script.Parent.Parent
local leaderboard = player:WaitForChild("leaderstats")
local currency1 = leaderboard:WaitForChild("Gold")

function onDied()
    print("Dead")
    currency1.Value = currency1.Value + 100
end

if game.Workspace.TutPrac.Viking.Humanoid.Heath <== 0 then
onDied()
end)
0
To note, this would only work once: When the player's character (re)spawns. (This code, I mean) TheeDeathCaster 2368 — 6y

Answer this question