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)
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.
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
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!