So this is sort of a follow up to my other post. I was just wondering, how would I find the killer of a humanoid within a local script contained in StarterPlayerScripts, and store it in a variable?
Sorry, this is so short, but it’s really just a sort of short question.
All answers are appreciated.
–EPIC_GameDev2
local char = script.Parent
local hum = char:WaitForChild(‘Humanoid’)
local cam = workspace.CurrentCamera
local head = script.Parent.Head
hum.Died:Connect(function()
local respawnTime = 5
print("Died!")
cam.CameraSubject = workspace.CurrentCamera --this is just a placeholder for the killers humanoid, which i want the players cam to lock on to when killed.
wait(respawnTime)
cam.CameraSubject = hum
There’s no global way to do this. You would have to create your own system to keep track of who did damage and who was the last one to do so - or who did the most and count others as assists.
Well you cant exactly get the best estimate of who exactly killed who, but your best bet is too find who is the closest player to the player that just died
So basically fire a remote when the player dies and then see who is closest using magnitude
edit : loop through workspace and then find the humanoid root part or torso of anything that is close to the humanoid root part of the person that just died and thats most likely the killer
you could do some further checks by maybe putting the person that just killed another person inside of a table and then reading off that table and checking if the people in the table have weapons or not
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("HumanoidRootPart") then
cam.CameraSubject =
wait(respawnTime)
cam.CameraSubject = hum
end
does this look right? if so what should i put in front of the cameras subject?
Use a remote event to handle the find of the person and then fire the remote back to the client who fired it for the camera changes
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("HumanoidRootPart") then
local killerHRP = v:FindFirstChild("HumanoidRootPart")
if killerHRP then
if (killerHRP.Position - player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude < 50 then
print("This guy is most likely our killer.")
end
end
end
end
Like in a detective story you can find who is the murderer by finding the tool they used to kill
using the closest player would only work for close-range weapons which is what you may not want.
you can use robloxs tagging system which uses an object value to store the variable of who last damaged the humanoid
Both script portions
function getKillerOfHumanoidIfStillInGame(humanoid)
-- returns the player object that killed this humanoid
-- returns nil if the killer is no longer in the game
-- check for kill tag on humanoid - may be more than one - todo: deal with this
local tag = humanoid:findFirstChild("creator")
-- find player with name on tag
if tag ~= nil then
local killer = tag.Value
if killer.Parent ~= nil then -- killer still in game
return killer
end
end
return nil
end
–In the sword script
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
Its a nice idea, to do a loop checking the closest players. but that can lead to errors trying to choose the correct one player, what if there are 2 players, one kill the humanoid, and the other is closer?
I think, ur question is lacking of context. This should be handled by events on the “killing method”
Hey, you solution worked and I didn’t have to use any remote events. after your script, i made the players camera focus on the KillerHRP and wait 5 seconds, then change the camera focus back to the players head.
If anyone wants the script, put this into StarterCharacterScripts as a local script:
local char = script.Parent
local hum = char:WaitForChild(‘Humanoid’)
local cam = workspace.CurrentCamera
local head = script.Parent.Head
hum.Died:Connect(function()
local respawnTime = 5
print(“Died!”)
local player = game.Players.LocalPlayer
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("HumanoidRootPart") then
local killerHRP = v:FindFirstChild("HumanoidRootPart")
if killerHRP then
if (killerHRP.Position - player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude < 50 then
cam.CameraSubject = killerHRP
wait(respawnTime)
cam.CameraSubject = head
end
end
end
end
No, this is not accurate. You might want to use tags to get who killed who. A person might be far away and kill someone else. If your method is used, it would not find the correct person.
Edit: By the way, your loop might take a toll on performance