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

How Do I Make This Script Work Like I Want It To?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)

3 answers

Log in to vote
1
Answered by
Relatch 550 Moderation Voter
9 years ago

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)
0
It did not work even if i remove the 3 at one of the end :( Anthony9960 210 — 9y
0
I don't know what you did, It works fine for me. Relatch 550 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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.

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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
0
when i was looking in my script i put h regularly instead of myself and frogot to change it when i put it in the script helpers so yah :3 Anthony9960 210 — 9y

Answer this question