I am wondering if there is a more efficient way to detect if a player is touching a part so that it does something.
function ontouch (hit) If hit == game.players.localplayer.Character:findFirstChild("Torso") then print ("ow") end script.parent.touched:connect(ontouch)
Only LocalScripts
can access LocalPlayer
, and Local Scripts don't work in Workspace.
You were missing an end
In my solution, I got rid of these errors for you. I'll be using a regular script inserted into a part in workspace.
I would suggest checking if the part's name is "Torso",
--Regular script function ontouch(hit) if hit.Name == "Torso"then print ("ow") end end Script.Parent.Touched:connect(ontouch)
I would suggest making sure that what touched the part was a Humanoid,
--Regular script function ontouch(hit) if not hit.Parent:FindFirstChild("Humanoid") then return end--Checks Humanoid if hit.Name == "Torso"then print ("ow") end end Script.Parent.Touched:connect(ontouch)
Check if it's a player,
--Regular script function ontouch(hit) if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end--Checks for Player if hit.Name == "Torso"then print ("ow") end end Script.Parent.Touched:connect(ontouch)
You could of course do this with arms and legs as well.
Hope I helped!
Good Luck!