Player always get several kills the first kill performed?

For some reason, every time I join my game, the first kill a player performs always gives several levels to the player and is counted as several kills, this never happens while in the studio or anytime else than the first kill. And I see nothing that could do this in my code.

local range = 5

game.ReplicatedStorage.Remotes.Punch.OnServerEvent:Connect(function(player,damage) -- when punched is fired
--// Damage player/NPC
print (player)
print (damage)
local dmgpoints = game.Players[player.Name].LevelsInfo.dmgpoints.Value
local totaldmg = dmgpoints * 1
damage = dmgpoints + 20
print (damage)


-- checks if it's a humanoid and deals damage
for i,target in pairs(game.Workspace:GetDescendants()) do
	if target:IsA("Humanoid") and target.Parent.Name ~= player.Name then
		if (target.Parent.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < Range then
			target.Health -= damage
			print (target.Parent.Name)
			-- checks if it's a humanoid and deals damage
			
			if target.Health <= 0 then -- when npc dies
				
				
			
				wait(1)
				target.Health = 100
	
					wait (0.5)
				
					-- exp/gold indicators
						if target.Parent.Name == "lvl1" then
							game.ReplicatedStorage.Remotes.expindicator:FireClient(player,10)
						elseif target.Parent.Name == "lvl2" then
							game.ReplicatedStorage.Remotes.expindicator:FireClient(player,50)
						elseif target.Parent.Name == "lvl3" then
							game.ReplicatedStorage.Remotes.expindicator:FireClient(player,110)
						end
					-- exp/gold indicators
					
					
					-- exp/gold rewards
						wait (1.5)
						print (player.Name .. "just killed" .. tostring(target.Parent.Name))
						if target.Parent.Name == "lvl1" then
							
								game.Players[player.Name].leaderstats.Coins.Value += 10
								game.Players[player.Name].LevelsInfo.Experience.Value += 10 
						
						

						elseif target.Parent.Name == "lvl2" then
					
								game.Players[player.Name].leaderstats.Coins.Value += 50
								game.Players[player.Name].LevelsInfo.Experience.Value += 50	
						
							
						elseif target.Parent.Name == "lvl3" then
							
								game.Players[player.Name].leaderstats.Coins.Value += 110
								game.Players[player.Name].LevelsInfo.Experience.Value += 110
							-- exp/gold rewards
							

						end
						game.ReplicatedStorage.Remotes.npckilled:FireClient(player,target.Parent.Name) -- checks if quest if active for specific npc
						wait (1)
						
					else
					
					
					end
				end
				
				
				
				
				
	
		
				
			end
	end
end)

The issue could be because you use “player.Name”.
Names can change and might perhaps be duplicate (not 100%).

Just replace all usages of player.Name with player.UserId.

The user is guaranteed to be unique for different users.

1 Like