Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

1 comment

0

umm... the script is still activated by NPCs, what do i do?

1 answer

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)

3 comments

0

Ew, you should be using GetPlayerFromCharacter.

0

Eh, didn't know that existed, i dont use roblox studio much anyway

0

The script dose exactly the same thing, what now?

This discussion is preserved as it was. New answers, comments, and votes are closed.