Getting player from player name not working

Hello everybody!

I wrote this code in ServerScriptService (SSS):

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			if character.Humanoid then
				local creatorValue = character.Humanoid:FindFirstChild("creator")
				if creatorValue then
					local killerName = creatorValue.Value
					local killer = Players:FindFirstChild(killerName)
					local deathPlayer = Players:FindFirstChild(player.Name)

					print("we are here")

					if killer and deathPlayer then
						killer.leaderstats.Time.Value += deathPlayer.leaderstats.Time.Value
						deathPlayer.leaderstats.Time.Value = 0
					else
						warn("Killer or death player not found.")
					end
				else
					warn("Creator value not found in Humanoid.")
				end
			end
		end)
	end)
end)

This code is supposed to work like this:

  1. Check when a player died
  2. Find out who killed who
  3. Give the killer the prey’s time and put the prey’s time at 0.

For some reason, I cannot get the killer’s name.

The output prints this:
we are here
Killer or death player not found.

Please help!

Thanks
-GreenTreeGamingYT

1 Like

What is creatorValue.Value? Are you sure it exists?

1 Like

If i’m not mistaken then the returned player will be the player who joined the game the latest. What you should do is have a local script inside the players character that sends a remote event to the server with the correct player.

Have you checked if your saving the players username or display name?

1 Like

We check that creatorValue exists, and it does.

Also, I am using the classic Roblox sword the uses the “creator” tag to store information on who the murderer was.

1 Like

That is an ObjectValue. If you do it exactly the way Roblox does it in that script, you can just define killer as:

local killer = creatorValue.Value
2 Likes

Ah, i might have it.

The creatorValue is an ObjectValue, the Value is a reference to the player itself, it’s not a string. Try using tostring(creatorValue.Value)

EDIT: You should use what @ProgrammingRottie suggested above.

2 Likes

Thank you so much, it works like a charm!

2 Likes