Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Workspace.tecknobladee.Punch.Handle.Damage:6: attempt to index nil with 'leaderstats'?

I want to make it so every rebirth you gain 0.25 dmg but an error occurred.

15:10:43.471 - Workspace.tecknobladee.Punch.Handle.Damage:6: attempt to index nil with 'leaderstats'

script.Parent.Touched:Connect(function(hit, player)
	local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if humanoid then
		humanoid:TakeDamage(player.leaderstats.rebirths.Value / 4 + 5)
	end
end)

2 answers

✓ Accepted answer

The Touched event only returns one parameter, and that the thing that hit the part. It doesn't return the player that touched the part. Since you're already checking if the thing that hit the part is a character, you can get the player from the character if it has one by using the Players:GetPlayerFromCharacter function.

EDIT: You can get the player's Character that is holding the tool by just using script.Parent's. Then you can use the Players:GetPlayerFromCharacter function to get the player from the Character.

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if humanoid then
		local CurrentCharacter = script.Parent.Parent.Parent -- You may need to change how many parent's you need to reach the Character
		local CurrentPlayer = Players:GetPlayerFromCharacter(CurrentCharacter)
		humanoid:TakeDamage(CurrentPlayer.leaderstats.rebirths.Value / 4 + 5)
	end
end)

Hope this helps.

15 comments

0

So how should I fix it?

0

I edited my answer with the fixed code. Hope it helps.

0

It says nil with leaderstats on number 7

0

Are you sure leaderstats exist inside the player? Try making variables before the TakeDamage function for the leaderstats

0

oh ok thank you

0

I tried making variables but it keeps on looking in workspace. how do i make it so it looks in the players files/

0

What do you mean it keeps looking in the workspace? It shouldn't be looking in the workspace. Can you send me a screenshot of your code?

0

local Players = game:GetService("Players") script.Parent.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid') if humanoid then local player = Players:GetPlayerFromCharacter(hit.Parent) local rebirths = player.leaderstats.rebirths humanoid:TakeDamage(rebirths.Value / 4 + 5) end end)

0

I putted it as a answer

0

Okay.. Are you trying to get the leaderstats from the player that was hit by the handle or from the player that is holding the handle?

0

from the player is holding

0

Ahh, okay that makes sense. My bad. I'll edit my answer.

0

Okay, I've edited my answer. Hope it works now.

0

Thanks it worked

0

No problem. Make sure to mark my answer as accepted.

local Players = game:GetService("Players")
	 
script.Parent.Touched:Connect(function(hit)
	    local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	    if humanoid then
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local rebirths = player.leaderstats.rebirths
	        humanoid:TakeDamage(rebirths.Value / 4 + 5)
	    end
	end)

This discussion is preserved as it was. New answers, comments, and votes are closed.