Hit Player is nil

Self explanatory.

I made a sword, and in the sword it uses ClientCaster . I put a global script inside and then I used a function from ClientCaster called HumanoidCollided which means the sword collided with a humanoid. This works perfectly, it takes damage away and does what it needs to do. Then I made a event and fired it to the client inside humanoidcollided with the parameter of the player and the hit player. The Event fires but inside the client script it errors out “Attempt to index nil with “name”” for the hit humanoid.

Now I am wondering how it’s indexing nil

Output:

LocalScript:36: attempt to index nil with 'Name'

Global Script

ClientCaster.HumanoidCollided:Connect(function(RaycastResult, HitHumanoid)
	if HitHumanoid and HitHumanoid ~= script.Parent.Parent.Humanoid then	
		if Debounce2[HitHumanoid] then
			return
		end
		local play_er = game.Players:GetPlayerFromCharacter(Tool.Parent)
		UntagHumanoid(HitHumanoid)
		TagHumanoid(HitHumanoid, play_er,script.Parent.Parent.Name)
		local Damage
		if combo == 1 then
			Damage = SettingsFolder.Slash1Damage.Value
		elseif combo == 2 then
			Damage = SettingsFolder.Slash2Damage.Value
		elseif combo == 3 then
			Damage = SettingsFolder.Slash3Damage.Value
		elseif combo == 4 then
			Damage = SettingsFolder.Slash4Damage.Value
		end
		
		Debounce2[HitHumanoid] = true
		print("Fired Client")
		GlobalEventFolder.Sword.PlayerKilled:FireClient(play_er,HitHumanoid.Parent)
		HitHumanoid:TakeDamage(Damage)
		if HitHumanoid.Health <= 0 then
			if play_er.Health ~= 100 then
				play_er.Health = play_er.Health + 25
			end
		end
		SoundsFolder.Hit:Play()
		wait(0.7)
		Debounce2[HitHumanoid] = false
	end
end)

LocalScript

EventFolder.Sword.PlayerKilled.OnClientEvent:Connect(function(Player, PlayerKilled)
	print("KILLED ".. PlayerKilled.Name)
	local BottomFrame = script.Parent.Parent
	local KillFrame = BottomFrame.KillFrame:Clone()
	KillFrame.KilledText.Text = "KILLED "..PlayerKilled.Name
	KillFrame.Parent = BottomFrame.KillPopups
	KillFrame.Visible = true
	KillFrame.KillSound:Play()
	game.Debris:AddItem(KillFrame,1)
end)

BTW, ignore how I fired the client in the wrong line. It was for testing.

The client you are sending remoteevent parameter is invisible for the client, remove the first parameter “Player” from the LocalScript connection and it should work

ClientCaster.HumanoidCollided:Connect(function(RaycastResult, HitHumanoid)
	if HitHumanoid and HitHumanoid ~= script.Parent.Parent.Humanoid then	
		if Debounce2[HitHumanoid] then
			return
		end
		local play_er = game.Players:GetPlayerFromCharacter(Tool.Parent)
		UntagHumanoid(HitHumanoid)
		TagHumanoid(HitHumanoid, play_er,script.Parent.Parent.Name)
		local Damage
		if combo == 1 then
			Damage = SettingsFolder.Slash1Damage.Value
		elseif combo == 2 then
			Damage = SettingsFolder.Slash2Damage.Value
		elseif combo == 3 then
			Damage = SettingsFolder.Slash3Damage.Value
		elseif combo == 4 then
			Damage = SettingsFolder.Slash4Damage.Value
		end
		
		Debounce2[HitHumanoid] = true
		print("Fired Client")
		local HitPlayer = game.Players:GetPlayerFromCharacter(HitHumanoid.Parent)
		GlobalEventFolder.Sword.PlayerKilled:FireClient(play_er,HitPlayer)
		HitHumanoid:TakeDamage(Damage)
		if HitHumanoid.Health <= 0 then
			if play_er.Health ~= 100 then
				play_er.Health = play_er.Health + 25
			end
		end
		SoundsFolder.Hit:Play()
		wait(0.7)
		Debounce2[HitHumanoid] = false
	end
end)

EventFolder.Sword.PlayerKilled.OnClientEvent:Connect(function(PlayerKilled)
	print(PlayerKilled)
	print("KILLED ".. PlayerKilled.Name)
	local BottomFrame = script.Parent.Parent
	local KillFrame = BottomFrame.KillFrame:Clone()
	KillFrame.KilledText.Text = "KILLED "..PlayerKilled.Name
	KillFrame.Parent = BottomFrame.KillPopups
	KillFrame.Visible = true
	KillFrame.KillSound:Play()
	game.Debris:AddItem(KillFrame,1)
end)

Am I doing it correctly? It still says nil though

Is PlayerKilled nil too? Also maybe HitPlayer is nil on the server in the first place

1 Like

Also are you testing it in the emulator

What do you mean emulator? If you mean roblox studio, yes. PlayerKilled is not nil and I think you are right about HitPlayer being nil on the server but I am not sure how I am going to fix that.

EventFolder.Sword.PlayerKilled.OnClientEvent:Connect(function(Player)
	print(Player) -- focus on this 
	--print("KILLED ".. PlayerKilled.Name)
	--local BottomFrame = script.Parent.Parent
	--local KillFrame = BottomFrame.KillFrame:Clone()
	--KillFrame.KilledText.Text = "KILLED "..PlayerKilled.Name
	--KillFrame.Parent = BottomFrame.KillPopups
	--KillFrame.Visible = true
	--KillFrame.KillSound:Play()
	--game.Debris:AddItem(KillFrame,1)
end)

I tested this by printing the player ( not the player that got hit ) and it still said nil

Like is the humanoid part of a real player

Just incase you were using it on an npc or something

1 Like

Yup I was testing it on a dummy. I used a emulator and it worked, sorry for that lol.

1 Like

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