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

How can i trigger a script via player, and not NPCs?

Asked by 5 years ago

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?

0
umm... the script is still activated by NPCs, what do i do? mrfrank79 30 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)

0
Ew, you should be using GetPlayerFromCharacter. User#19524 175 — 5y
0
Eh, didn't know that existed, i dont use roblox studio much anyway Azure_Kite 885 — 5y
0
The script dose exactly the same thing, what now? mrfrank79 30 — 5y
Ad

Answer this question