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

Why doesn't this brick actually hurt the player it touches?

Asked by 8 years ago

I am rather new to lua, and i cant figure out whats wrong with this script. May you please help? Here is my code:

script.Parent.Touched:connect(function()
touched.Humanoid.Health = touched.Humanoid.Health - 50
end)

1 answer

Log in to vote
1
Answered by
Uroxus 350 Moderation Voter
8 years ago

Your Problem

What you're doing is you never actually reference the player, you're simply only using touched when trying to find the humanoid which would throw back an error because a humanoid wouldn't exist.


The Solution

You've got off to a good start, your function would actually work, however it's probably best to pass player or plr through at the end like so; script.Parent.Touch:connect(function(plr)) Using the plr you then can change their health using the same code as you have but just change the touched to plr. We can make this a bit more efficient too, just to make sure that what ever touches the brick is actually a player or some kind of NPC and not some other thing. This can be done by looking for a humanoid from the object or player the fired the function.

script.Parent.Touched:connect(function(plr)) -- Create the function
    local humanoid = plr.Parent:FindFirstChild("Humanoid") -- See if there's a humanoid in the object that touched the part
    if (humanoid ~= nil) then -- If there is then
        humanoid.Health = humanoid.Health - 50 -- Damage the player / npc
    end
end

Conclusion

If this answered your question then accept my answer with the button on the right. If it doesn't work please do tell me and if you'd like a better explanation or don't understand don't hesitate to ask!

0
Thank you :D coolburton00 5 — 8y
Ad

Answer this question