FindFirstChild not working when using a variable

I am trying to make a kills leaderboard and am editing the script for a rocket launcher so that it gives the player a point for every kill. The script is specifically the one which is copied into any rocket that is fired.

Below is the portion of the script defining the function that kills players (other than the one who fired the rocket, to allow for rocket jumping). The CreatorTag value is the name of the player who fired the rocket, so I used it to find the player and give them points.

local function OnExplosionHit(hitPart)
	if hitPart then
		local _, humanoid = FindCharacterAncestor(hitPart.Parent)
		if humanoid and humanoid.Health > 0 and not hitPart:IsDescendantOf(CreatorTag.Value.Character) then
			CreatorTag:Clone().Parent=humanoid
			humanoid:TakeDamage(100)

			-- everything below this line is what I added to the script

			local character = CreatorTag.Value
			local player = game.Players:FindFirstChild(character)
			print(player.Name .. " got a kill")
			
			player.leaderstats.Kills.Value = player.leaderstats.Kills.Value + 1
		end
	end
end

…and here it is in action:

As you can see it doesn’t work, but if I change the parameter of FindFirstChild to my actual player name and not the variable which should contain it, it works just fine.

image

I seriously don’t know what I’ve done wrong. Is this a bug? I’m sure the CreatorTag value is the exact same as my name. Does anybody know what’s going on and what I can do?

Print the value of

character

and see what it is

If CreatorTag.Value is an Instance then this wouldn’t function because Instance:FindFirstChild takes a string, not an object. Do character.Name. Furthermore, judging from your script, CreatorTag.Value is already a Player Instance, so just do:

local player = CreatorTag.Value
2 Likes

The value is levy25 as it should be
image

This fixed everything, thanks! I didn’t really understand what CreatorTag.Value really was since I didn’t create any of that myself.

1 Like