Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Unable to index nil with humanoid how to fix?

Asked by 2 years ago
Edited 2 years ago
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

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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)
0
Thx dylancrazy88 20 — 2y
0
but i want it to damage the player only if it hits the root part of the player dylancrazy88 20 — 2y
0
just so it doesn't do too much damage which is not intended dylancrazy88 20 — 2y
0
this script will get the player's humanoid, so you don't need to get the player's root part. Sarturex857 109 — 2y
View all comments (3 more)
0
hello? dylancrazy88 20 — 2y
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 dylancrazy88 20 — 2y
0
i edited the script. I made a cooldown using a debounce, it should work now. Sarturex857 109 — 2y
Ad

Answer this question