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

How To Check If A Player Is A NPC?

Asked by 5 years ago

In My Game That I'm Working On I Have A Teleporter To A Pirate Ship That Has A Bunch Of Wandering NPC's.My Problem Is That The Go Through The Teleporter Into The Lobby And Annoy The People In The Lobby. Can Someone Help Me Make A Filtering System. (Also I Didn't Make The Teleport Part Of The Script)

local Teleport = "Lentilkac58-Easy-Teleport-2" --Put the name of the Part between the ""s.
function Touch(hit) --Indicates that the Part has been Touched.
    if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:findFirstChild(Teleport).Locked = true --Checks Debounce.
    local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
        hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false end end --Takes you there and Ends the Function.
script.Parent.Touched:connect(Touch) --Listens out for Touchers.

1 answer

Log in to vote
1
Answered by
DogeIXX 172
5 years ago

As I can understand is that you don't want NPCs to go through the teleporter. That is easy to handle, just add this code:

for i, plr in ipairs(game.Players:GetChildren()) do
if hit.Parent.Name == plr.Name then
--Continue here
break
else
print("Not a player!")
end
end

I wrote this on mobile and did not test this, so it may have errors. If it does, feel free to tell me what the error is.

1
An easier way to do this is ```if not game.Players:GetPlayerFromCharacter(hit.Parent) then print('not a player!') end ``` Shawnyg 4330 — 5y
0
^ DeceptiveCaster 3761 — 5y
0
That's going to give lots of false positives. Any time a part that is inside a character accessory is the 'hit' part, it's going to print("not a player!"). This is also a very expensive check to do on Touched, it loops over all the players. The fast way is to put NPCs into a table as keys when you create them, so you have an O(1) map of what is an NPC. EmilyBendsSpace 1025 — 5y
0
Didnt Work ricelicker 52 — 5y
Ad

Answer this question