I want a brick when you stand in it you get hurt, but my script only count if your touching help?
function onTouched(part) local MySelf = part.Parent:findFirstChild("Humanoid") if MySelf~=nil then h.Health = h.Health - 3 end end script.Parent.Touched:connect(onTouched)
To take damage from the player, you need to find hit.Parent, not part.Parent. part.Parent looks for the part's parent, and part isn't even explained. Also, you used h, and you didn't explain what h is.
An easier way to take damage from the humanoid would be TakeDamage. TakeDamage is a property of humanoid that takes damage(health) from it. That can be set up using Humanoid:TakeDamage(number).
script.Parent.Touched:connect(function(hit) local chr = hit.Parent if game.Players:GetPlayerFromCharacter(chr) and chr:FindFirstChild("Humanoid") then local plr = game.Players:GetPlayerFromCharacter(chr) chr.Humanoid:TakeDamage(3) --Takes 3 damage/health from hit.Parent end end)
I would like to point out that both part
and hit
are exactly the same!
Every touched event has one built in parameter. That parameter is equal to the part that hit the brick with the touched event connected to it. Parameters are very similar to variables in the sense that they stand for something. What you name them has absolutely nothing to do with what they equal.
The following pieces of code will behave exactly the same.
function onTouch(part) print("I got touched by "..part.Name) end script.Parent.Touched:connect(onTouch)
=
function whenITouchDisBrick(hit) print("I got touched by "..hit.Name) end script.Parent.Touched:connect(whenITouchDisBrick)
=
function asdfqwer(dufohohae) print("I got touched by "..dufohohae.Name) end script.Parent.Touched:connect(asdfqwer)
These are all exactly the same!
Other than that, MrSmenryBackup is correct. You used an undefined (or nil) variable on line 7. Since you already have a variable for the Humanoid
, just use that instead of h
.
If this is the whole script, then the only error I see is that variable 'h' is not defined/assigned to any values.
function onTouched(part) local MySelf = part.Parent:findFirstChild("Humanoid") if MySelf~=nil then h.Health = h.Health - 3 -- Script: OK, so the coder tells me to use 'h'... 'h??' What is that??? end end script.Parent.Touched:connect(onTouched)
Since the variable "MySelf" is already defined as the humanoid, replace 'h' with 'MySelf' to damage the humanoid.
local MySelf = part.Parent:findFirstChild("Humanoid") if MySelf ~= nil then MySelf.Health = MySelf.Health - 3 -- Script: OK, so the coder tells me to use 'MySelf'. And 'MySelf' is defined as "part.Parent:findFirstChild("Humanoid")." I will subtract 3 from the value of "Health" within 'MySelf.' end