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

Player Won't take damage?

So upon writing a quick and easy damage script,I keep on getting this part of an error "Human(a nil value)"

Here's the script:

--Upon stepping on the glass shard, damage will be taken.
local Human = script:FindFirstChild("Humanoid") --Required for the damage to be taken
local GlassShard = script.Parent --The object that does the damage

GlassShard.Touched:Connect(function()
	Human:TakeDamage(2) --The player will take 2 damage points upon touching the shard
end)

Please tell me what's wrong with this.I don't see anything wrong.

1 answer

✓ Accepted answer

Hello, RoccoAttackYT!

--Upon stepping on the glass shard, damage will be taken.
-- Removed this human because the script don't have a child called "Humanoid"
local GlassShard = script.Parent --The object that does the damage

GlassShard.Touched:Connect(function(part) -- part is now a reference to what touched
	Human = part.Parent:FindFirstChild("Humanoid") -- Tryes to refer to touch part's parent humanoid
	if (Human) then --Test if is player(or NPC) based on humanoid existing
		Human:TakeDamage(2) --The player will take 2 damage points upon touching the shard
	end
end)

If you need more help about this code, add me on Roblox!

Good Luck with your games

5 comments

0

Thanks man.I modified it a bit so I hope you don't mind and thanks for the good luck.Good luck to you to!

0

why is Human global

0

? Why I choose "Human" as the var name? I just copyed from the original script

0

Welp, The humanoid goes before touch, and add a argument function(here) which distributes the function needed for the part.touch:connect thing to work but since someone solved this.

0

wait the humanoid varible goes like inside the function

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