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 .
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)
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.
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.
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”?