Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why is it not giving me the cash when I kill npc?

So I have a npc that you can kill and I put a server script inside it that makes it so when you kill the npc it gives you cash (this is a naruto game so cash = ryo). But the script doesn't give me money when I kill the npc, heres the script:

game.Players.PlayerAdded:Connect(function(player)
	local Itachi = script.Parent -- npc model
	local Humanoid = Itachi.Humanoid -- humanoid inside the model
	local Ryo = player.leaderstats.Ryo -- the money
	local RyoReward = 250000 -- amount of money I want to give when npc dies

	if Humanoid.Health == 0 then -- I have tested this and if it is ">=" it gives cash when player joins the game, but I want it to only give cash if the npc dies
		Ryo.Value = Ryo.Value + RyoReward
	end


end)

1 answer

✓ Accepted answer

The problem is you are only checking once and only once this can be solved with an easy loop or event, I will show you how to do this with a GetPropertyChangedSignal() event. Like so:

obj:GetPropertyChangedSignal("Position"):Connect(function()
    print("Position changed!")
end)

In you case it will be:

game.Players.PlayerAdded:Connect(function(player)
    local Itachi = script.Parent -- npc model
    local Humanoid = Itachi.Humanoid -- humanoid inside the model
    local Ryo = player.leaderstats.Ryo -- the money
    local RyoReward = 250000 -- amount of money I want to give when npc dies
    Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
        if Humanoid.Health == 0 then
            Ryo.Value = Ryo.Value + RyoReward
        end
     end)
end)

1 comment

0

tysm! it worked, I have been trying to script something like this forever

This discussion is preserved as it was. New answers, comments, and votes are closed.