Hi! so i am wondering how to make a script that when you kill an npc you get money. i know it sounds simple but it’s very hard for me, for some reason. i’ve been looking everywhere for a script that would work but they just don’t beacuse my sword is made from scratch and not taken from free models and that’s why it doesn’t work. i would really appreciate the help!
You could add a script on the npc with that to get the died event and when you make damage to an npc, it store the player who damaged him on a object value so when died, the script look on the value:
script.Parent.Humanoid.Died:Connect(function()
local Player = script.Parent:FindFirstChild(“The Name of your value”).Value
end)
ok but how do i store who killed the npc?
you can create a damage script!!
For my game, every 3 kills rewards the player with 5 points. How i did it was pretty simple
Firstly, i have a module i use to damage the enemy (i used raycasting to damage. The enemy is either another player or an NPC)
Secondly, i check if the enemies health is less than or equal to 0 (meaning the target has died). If the target is died i give the player 5 points. Its reallly easy for me because i already have the player.
hitHum:TakeDamage(damage)
module.Stun(hitChar, 12, 5, 1)
if hitHum.Health <= 0 and hitPlr then
hum.Health = hum.MaxHealth
event:FireAllClients("Heal", char)
-- removed some code cause its not relevant to the topix
if hitPlr then
plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
local points = folders.points:WaitForChild(plr.Name.."Points")
points.Value += 1
if points.Value == 3 then
plr.leaderstats.Points.Value += 5
points.Value = 0
end
end
end
heres example code from my module.
plr represents the player thats doing damage, hitPlr represents the player that is receiving the damage.
I got the damaged player by locating the character, and using game.Players:GetPlayerFromCharacter(hitChar)
(hitChar being the targetted players character)
I hope this helps you, if not i can try explaining more!
Well actually without doing the steps above you can precalculate if the NPCs’ health is below zero after the damage has been given and apply the damage afterwards. This way you’d have the player who has given the latest damage to the NPC.
I hope I explained it, I can’t really give an example code right here because it depends on how you apply the damage.
i can’t understand probably beacuse i am still learning lua. but all i need is that when an npc is killed then the player who killed the npc gets money
you’d have to get the player that killed the NPC, so either you’d give the cash in your damage script or find a way to locate the player that killed the dummy in a seperate script.
i finally found out how to do it!!
Do a script in your sword like that:
Sword.Touched:Connect(function(hit)
if hit.Parent then
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("The Name of your value").Value = game.Players:FindFirstChild(Sword.Parent.Parent.Name)
end
end
end)