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

Humanoid not a valid member of script?

Asked by 5 years ago

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()
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 SuperSamyGamer 316 — 5y

3 answers

Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
great work at explaining and great answer too User#23252 26 — 5y
0
thx mc3334 649 — 5y
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. User#24403 69 — 5y
0
Because it was a good answer and someone liked it. Why would you downvote it JUST BECAUSE it has an upvote mc3334 649 — 5y
View all comments (5 more)
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. mc3334 649 — 5y
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. DeceptiveCaster 3761 — 5y
0
I hope you realize that .Touched:Connect is a coroutine mc3334 649 — 5y
1
The script would break if a Humanoid doesn't exist because the debounce would never reset back to false. PhantomVisual 992 — 5y
0
True, thanks for actually explaining to me instead of just telling me my answer is shit out of nowhere. I actually appreciate that. mc3334 649 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
Log in to vote
-1
Answered by 5 years ago
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

Answer this question