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

Unable to index nil with humanoid how to fix?

local DamagePart = script.Parent
local Humanoid = Player.Humanoid
local function DamagePlayer()
	DamagePart.Touched = Player.Humanoid:TakeDamage(10)
end

while true do
	wait(0.65)
	DamagePlayer()
end

i'm trying to make a part that when touched damages the player on a loop it doesn't work though i want to put the script in the npc so it can damage the player or any npc that has the same humanoid name as the player it says unable to index nil with humanoid how to fix?

1 answer

You should get the value of 'Player' because in your script is something nil. If you don't have it in your script, it will break it and it would not work.

Also, that's not how you use the Event '.Touched', Events works as a connector to a function. You should be doing this:

local DamagePart  = script.Parent
local db = false

local number -- the amoun(in numbers) that you want for it to wait.

DamagePart.Touched:Connect(function(hit)
 if not db then 
   db = true
   if hit.Parent:FindFirstChild('Humanoid') then
       hit.Parent.Humanoid:TakeDamage(10)
   end
   task.wait(number)
   db = false
 end
end)

7 comments

0

Thx

0

but i want it to damage the player only if it hits the root part of the player

0

just so it doesn't do too much damage which is not intended

0

this script will get the player's humanoid, so you don't need to get the player's root part.

0

hello?

0

it's that i want it to damage the player if it touches 1 part and it'll have to wait a certain period of time before damaging them once again

0

i edited the script. I made a cooldown using a debounce, it should work now.

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