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

Humanoid not a valid member of script?

Trying to make a part do damage to a player when touched but the output is saying that the "Humanoid isn't a valid member of script" and I don't really understand what it means.

And if you're wondering what I wrote then here it is.

function part() 
	part = script.Parent
	if script.Parent.Touched then
		script.Parent.Parent.CanDamage = true 
		game.Players.LocalPlayer.Humanoid:TakeDamage(15)
		if script.Parent.TouchEnded then 
			script.Parent.Parent.CanDamage = false
		end
	end
end

part()

1 comment

0

You can't put an event inside an if condition, I think this is a server script so you can't do game.Players.LocalPlayer, and Humanoid is a child of the player's character, not the player itself

3 answers

✓ Accepted answer

There are a few problems with this code. One, is there is no need to put it all in a function. You can simple do something like this. Put this in a serverscript under the part you w ant to damage people:

local db = false
script.Parent.Touched:Connect(function(obj)
	local hum = obj.Parent:FindFirstChild("Humanoid")
	if not hum then return end
	if db == true then return end
	db = true
	hum:TakeDamage(15)
	wait(.5)
	db = false
end)

This proposes quuick and easy solution to your problem. I also added a debounce so it doesent do 3 million damage out of nowhere when you only want 15.

9 comments

0

great work at explaining and great answer too

0

thx

0

"There are a few problems with this code. One, is there is no need to put it all in a function. You can simple do something like this." is stupid. Why does this have an upvote.

0

Because it was a good answer and someone liked it. Why would you downvote it JUST BECAUSE it has an upvote

0

incapaxx, you have been a jerk to me since we met. Could you just go mind your own buissness. Go find your own questions to answer.

0

mc I actually agree with incapaxx; event listeners CAN be embedded in a function and there is no problem with doing so. However, using events in a statement is a rookie mistake, so I agree with that. Coroutines may also be used in this situation.

0

I hope you realize that .Touched:Connect is a coroutine

1

The script would break if a Humanoid doesn't exist because the debounce would never reset back to false.

0

True, thanks for actually explaining to me instead of just telling me my answer is shit out of nowhere. I actually appreciate that.

You didn't define the player's character. This is a common mistake though, so don't worry.

local canDamage = script.Parent.CanDamage.Value -- change this to the value of the candamage value

function partTouched(hit) 
        local part = script.Parent
        canDamage = true
	if canDamage == true then
		hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)
	end
end

function partUntoched()
	canDamage = false
end

script.Parent.Touched:connect(part)
script.Parent.TouchEnded:connect(partUntouched)
debounce = true
script.Parent.Touched:Connect(function(hit)
if debounce then
debounce = false
if hit.parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 15
wait(1)
debounce = true
end
end
end)

this will only damage the humanoid when it can find a humanoid and it got a 1 sec cooldown from the debounce if you dont want the cooldown for some weird reason then just delete them and the wait

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