Trying to find the player that killed an NPC

Trying to define player in my script but i don’t know how to find the person who killed the npc. Also i am using Lootplan
here is the script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = game.ReplicatedStorage.Tools

local LootPlan = require(ReplicatedStorage.LootPlan)
local DropPlan = LootPlan.new("single")

DropPlan:AddLoot("Apple of Healing", 99)
DropPlan:AddLoot("Nothing", 1)


local Humanoid = script.Parent.Humanoid
function Dead()
	local Backpack = Players.Player.Backpack
	local dropType = DropPlan:GetRandomLoot()
	if dropType ~= "Nothing" then
		Tools[dropType]:Clone().Parent = Players.Player.Backpack
	end
end
Humanoid.Died:Connect(Dead)
1 Like

You can give a random value inside your npc with a player’s name and then index it in the model from the value’s name, something like

make your weapon detect when it kills the npc, then if it detects it

if (YourNPCModel.Humanoid.Health - TheDamageYouGive) == 0 then
    YourNPCModel.Humanoid:TakeDamage(TheDamageYouGive)

    local PlayerName = Instance.new("StringValue")
    PlayerName.Name = Player.Name --Can possibly be Character's name too but you will have to     index in workspace
    PlayerName.Parent = YourNPCModel
end

and inside your dead script you then can now do the check

local Humanoid = script.Parent.Humanoid
function Dead()
	local Backpack = Players.Player.Backpack
	local dropType = DropPlan:GetRandomLoot()

    local getPlayer = script.Parent:FindFirstChildOfClass("StringValue") -- Checks if there is a value
    if getPlayer then
    	if dropType ~= "Nothing" then
    		Tools[dropType]:Clone().Parent = Players[getPlayer.Name].Backpack
    	end
    end
end

A cool alternative could be an Attribute if you want to check it out too

1 Like

Thanks for the help. Does the script inside the weapon need to be a normal or local? Also, does this only work with one npc because my game has multiple different enemies

1 Like

If you want to generate damage on something inside a game it must be a “normal” script
(Commonly known as Server Scripts, Server Scripts are used so the entire server sees that you are doing damage, if you use the Local Script you will be the only one who sees the damage and even make the Server Scripts not even react to your progress)

And yes it can work with multiple NPCs, AS LONG as you give them the same code lines

1 Like

So if i change all of their names to npc and put their names in the script as npc then it will work for all of them?

1 Like

Also do i need to define Npc and player? It says attempt to index nil with humanoid in the weapon script

1 Like

You can index all the NPCs through a script and detect if they die with Humanoid.Died, but every single one has to get it’s own variable for it to work.

You can use tables to index them all with a single name, but if you want a simpler thing to do just index them manually, but you will have to change their names

Another solution I could offer is just having the same script inside each NPC, so you don’t need to change the NPC’s name

1 Like

@Shadnatic ok so since im fairly new to scripting, JUST TO BE CLEAR, if i put this script inside of all my weapons (changing the damage value for each one)

if (Npc.Humanoid.Health - 1) == 0 then
	Npc.Humanoid:TakeDamage(1)

	local PlayerName = Instance.new("StringValue")
	PlayerName.Name = Player.Name --Can possibly be Character's name too but you will have to     index in workspace
	PlayerName.Parent = Npc
end

And then put this script into all the npcs named the same thing

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = game.ReplicatedStorage.Tools

local LootPlan = require(ReplicatedStorage.LootPlan)
local DropPlan = LootPlan.new("single")

DropPlan:AddLoot("Apple of Healing", 99)
DropPlan:AddLoot("Nothing", 1)


local Humanoid = script.Parent.Humanoid
function Dead()
	local Backpack = Players.Player.Backpack
	local dropType = DropPlan:GetRandomLoot()

	local getPlayer = script.Parent:FindFirstChildOfClass("StringValue") -- Checks if there is a value
	if getPlayer then
		if dropType ~= "Nothing" then
			Tools[dropType]:Clone().Parent = Players[getPlayer.Name].Backpack
		end
	end
end

both in normal aka server scripts, then im good to go? tell me if i need any changes and how to make them if i do because i dont know how to define those things in the weapon script or connect the weapon script to the npc script

1 Like

If you don’t know how to define the damage inside the weapon script then it won’t work, you cannot just directly copy and paste my weapon damage script into your weapons, it was an example, if you use it in it’s current position it won’t do anything.

If you didn’t script your weapons just check the scripts of it and try to understand when or how they damage entities and if you find it then you can add the example script into it.

1 Like

Do you think maybe something like this could work? I was having trouble defining player

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid") 
	if (humanoid ~= nil) then	-- if a humanoid exists, then
	humanoid.Health = humanoid.Health-1	-- damage the humanoid 
	local Workspace = game:GetService("Workspace")
	local Npc = Workspace.Npc
	local Players = game:GetService("Players")
	local Handle = script.Parent
	local Player = game.Players:FindFirstChild(Handle.Parent)
	if (Npc.Humanoid.Health - 1) == 0 then
		local PlayerName = Instance.new("StringValue")
		PlayerName.Name = Player.Name --Can possibly be Character's name too but you will have to     index in workspace
		PlayerName.Parent = Npc
		end
	end
end

script.Parent.Touched:connect(onTouch)
1 Like

Here, this should work

local Handle = script.Parent
local Player = game.Players:FindFirstChild(Handle.Parent)
local Character = Player.Character
local DamageToProduce = 1
local Debounce = true

local function onTouch(part)
	if Debounce then --We check if it can trigger to avoid and limit it from doing infinite constant damage
		Debounce = false
		
		local EnemyToHit = part.Parent -- Not called NPC to generalize
		local EnemyHumanoid = part.Parent:FindFirstChildOfClass("Humanoid")

		if EnemyHumanoid and EnemyToHit ~= Character then --Check if Enemy isn't your character

			if (EnemyHumanoid.Health - DamageToProduce) <= 0 then --Check if Damage will kill him or not before doing damage
				EnemyHumanoid:TakeDamage(DamageToProduce) --Produce Damage AFTER checking
				local PlayerName = Instance.new("StringValue")
				PlayerName.Name = Player.Name
				PlayerName.Parent = EnemyToHit
			else
				EnemyHumanoid:TakeDamage(DamageToProduce) --If hit didn't kill then don't create string and just let it take damage
			end
		end
		
		spawn(function()
			wait(0.3)
			Debounce = true
		end)
		
	end
end
Handle.Touched:Connect(onTouch())
1 Like