I'm an early scripter, but i wanted to know how you could detect a player touch using scripts. I'm making a game with ghosts in which chase you, and when touched, they die and respawn. The problem is that I include other NPCs in my game which also trigger the script. Here is the script i am using...
ghost = script.Parent.Parent function onTouched(hit) local b = hit.Parent:FindFirstChild("Humanoid") if b ~= nil then ghost.Humanoid.Health = 0 end end script.Parent.Touched:connect(onTouched)
Also, the script being used is contained insided each humanoid 'LeftArm','UpperperTorso', 'Head' etc.
Any ideas on how to fix this?
You can use if statement to check if the one that touch it is a player
ghost = script.Parent.Parent function onTouched(hit) if game.Players:FindFirstChild(hit.Parent.Name) then local b = hit.Parent:FindFirstChild("Humanoid") if b ~= nil then ghost.Humanoid.Health = 0 end end end script.Parent.Touched:connect(onTouched)