Hello,
I have this script that detects when a dummy/npc has died, and it is unable to fire the event due to it not being able to find a player instance. I tried game.players.localplayer, etc. Still can’t get it to work!
If anyone has any ideas on how to fix, lmk!
Server script in a npc:
local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local human = npc:WaitForChild("Humanoid")
local plrsHit = {}
local Animations = {
["Attack"] = human:LoadAnimation(script:WaitForChild("anim"))
}
local maxDistance = math.huge
local plr = game:GetService("Players").LocalPlayer
npc.Humanoid.Touched:Connect(function(touch)
if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] and script.Parent.Humanoid.Health > 0 then
plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true
touch.Parent.Humanoid:TakeDamage(10)
Animations.Attack:Play()
wait(1)
plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = false
end
end)
npc.Humanoid.Died:Connect(function()
wait(2)
local Type = "EnemDeath"
game:GetService("ReplicatedStorage"):WaitForChild("DeatghEv"):FireClient(Type)
end)
npc.Humanoid.Died:Connect(function()
wait(2)
local player = ...
local Type = "EnemDeath"
game:GetService("ReplicatedStorage"):WaitForChild("DeatghEv"):FireClient(player,Type)
end)
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")
local DeatghEv = RS:WaitForChild("DeatghEv")
local npc = script.Parent
local human = npc.Humanoid
local hroot = npc.HumanoidRootPart
local Animator = human.Animator
local Animations = {
["Attack"] = Animator:LoadAnimation(script.anim)
}
local maxDistance = math.huge
local debounce = false
local plr
hroot.Touched:Connect(function(touch)
if debounce == false then
if PS:GetPlayerFromCharacter(touch.Parent) and human.Health > 0 then
plr = PS:GetPlayerFromCharacter(touch.Parent)
local char = plr.Character
local playerhuman = char.Humanoid
playerhuman:TakeDamage(10)
Animations.Attack:Play()
wait(1)
end
debounce = true
wait(1)
debounce = false
end
end)
human.Died:connect(function()
wait(2)
local Type = "EnemDeath"
DeatghEv:FireClient(plr,Type)
end)
You can’t use in server script:
game.Players.LocalPlayer
Do not use WaitForChild in the server script except for the event, as it is unnecessary.
Ah wait, I ran into a problem. If you have multiple robots and kill one, it works. If you kill another, you get the error: lua FireClient: player argument must be a Player object
Also adding on, the event only fires if the ai has attacked you atleast once. So if you kill it from afar without attacking you, the event doesn’t fire since there is no player found.
You need to specify a player to send the data to. Which player do you want the event to fire too? If you want it to fire to all players use FireAllClients.
Edit:
A way to store the player that damages an NPC is called a Creator tag. Basically when a player dmgs the NPC you create an object value named “Creator” that is set to the player. This way when the NPC dies you can look for an object value named Creator and find the player who killed the NPC with it. I’d recommend looking into this.
tl;dr;
When damaging the NPC, like in a tool or something, you need to store who dmged the NPC in the NPC, so when the NPC dies you can get that info.
I don’t know how I would check to see if a player damaged it, I understand I could try human.HealthChanged, but how would I detect it was a player that damaged it?
You need to store that info yourself. For example, if you have a sword being held by a player’s character, and that sword does dmg to an NPC, when dealing the dmg you also store who did it (player holding tool in this case) in the NPC.
How would I detect it actually doing damage though? Because if I just use tool.activated and fire a event, even if they don’t do damage, then it’ll store players for no reason.
local tool = script.Parent
local PS = game:GetService("Players")
local debounce = false
tool.Handle.Touched:Connect(function(hit)
if PS:GetPlayerFromCharacter(script.Parent) and debounce == false then
local plr = PS:GetPlayerFromCharacter(script.Parent)
if hit.Parent:FindFirstChild("Humanoid") and and not plr:IsA("Player") then
local enemyhuman = hit.Parent.Humanoid
if not enemyhuman:FindFirstChild("Player_Tag") then
local tag = Instance.new("ObjectValue", enemy)
tag.Name = "Player_Tag"
tag.Value = plr
end
debounce = true
wait(1)
tag:Destroy()
debounce = false
end
end)
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")
local DeatghEv = RS:WaitForChild("DeatghEv")
local npc = script.Parent
local human = npc.Humanoid
local hroot = npc.HumanoidRootPart
local Animator = human.Animator
local Animations = {
["Attack"] = Animator:LoadAnimation(script.anim)
}
local maxDistance = math.huge
local debounce = false
hroot.Touched:Connect(function(touch)
if debounce == false then
if PS:GetPlayerFromCharacter(touch.Parent) and human.Health > 0 then
local player = PS:GetPlayerFromCharacter(touch.Parent)
local char = player.Character
local playerhuman = char.Humanoid
playerhuman:TakeDamage(10)
Animations.Attack:Play()
wait(1)
end
debounce = true
wait(1)
debounce = false
end
end)
human.Died:connect(function()
wait(2)
local plr human.Player_Tag.Value -- This
local Type = "EnemDeath"
DeatghEv:FireClient(plr,Type)
end)