Script can't find killer of player?

Hello i’m trying to make a script which gets the killer and adds the value as +1 for a quest but it doesn’t work. I’ve spent so much time on this :sweat_smile:.

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Died:Connect(function()
			local lastKiller = humanoid:FindFirstChild("creator")
			if lastKiller and lastKiller.Value then
				local killerPlayer = Players:GetPlayerFromCharacter(lastKiller.Value)
				if killerPlayer then
					print(killerPlayer.Name .. " killed " .. character.Name)

					local killProgress = killerPlayer:FindFirstChild("KillProgress")
					if killProgress and killProgress:IsA("IntValue") then
						killProgress.Value += 1
						print(killerPlayer.Name .. "'s KillProgress:", killProgress.Value)
					end
				end
			end
		end)
	end
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)
2 Likes

If you give the player that died a StringValue of their killer’s name, you can just use that value and not need their entire character model.

1 Like

Try using WaitForChild(“Humanoid”) instead of FindFirstChild(“Humanoid”).

1 Like

In addition to the advice above, consider not recording the killer as a character Model reference. You can end up with a race condition in a scenario where two players kill trade, and their characters despawn. It would be safer to record a Player reference, player.Name, or player.UserId, so you don’t have to worry about GetPlayerFromCharacter() failing because the character has been destroyed. This is not likely to be the problem you’re encountering first, but rather something else that could come up and make your code not always record kills.

1 Like

Thanks for the advice I will add to it.

Hello I just did that it works perfectly except for the part where it finds the value and finds the player?

game.Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Char)
		local Humanoid = Char:FindFirstChild("Humanoid") 
		if Humanoid then
			Humanoid.Died:Connect(function()
				for i,stringVal in pairs(Char:GetChildren()) do
					if stringVal.Name == "Killedby" and Plr:FindFirstChild("KillProgress") then 
						local Killer = game.Players:FindFirstChild(stringVal.Value) 
						local Kills = Killer.KillProgress 
						Kills.Value += 1
						print("Added")
					else
						warn("No value or smth")
					end
				end
			end)
		end
	end)
end)

Hey, I would recommend that you find the killer through the remote event sent from the weapon to kill a player or just through the weapon. Like take the damage and check if the player has greater than 0 health, if not then your killer is the guy who held the weapon.
*EDIT: If you want to keep your current system, then I guess make sure “stringVal.Value” changes before the player dies.

it does I made sure I’ll try again

Hmm, I would spilt your second if statement into two and place print signals to debug. If everything works right except the StringVal then I supposed if you know the player died by another player, you could loop until StringVal has a value. Also is the normal player supposed to have a “KillProgress”?

No its only for certain quests if they dont have one of them its not there. The only reason I made it was for these quests

So why are you checking if the normal player has a “killprogress”?

So if the killer doesnt have the certain quest it wont break

But in your script your checking if the victim has the “killprogress”, correct me if I am wrong.

1 Like

I THINK YOU FOUND IT! I will test right now!

1 Like

YOU FOUND IT TYSM! All it takes is a bit of teamwork :slight_smile:

You are very welcome! Indeed it does.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.