So I've been trying for very long to make it so my teleporters don't teleport humanoid npcs. I couldn't think of a solution, find any youtube videos, wikis or any code that would seperate npcs from actual players for my teleporters. This is essentially a game breaking issue I need to fix since what happens is, it teleports my npcs and also causes a bunch of errors. The error I get seems to have something to do with the "Locked" word in the code.
If anyone knows the show Doctor Who, this might help you give me the right answer. I don't want daleks to step on a teleporter and get teleported to the tardis I only want real players able to do this.
Edit: Oh and I don't want them anchored.
The code below is for the teleporters
local Teleport = "te4" function Touch(hit) if not script.Parent.Locked and not script.Parent.Parent:FindFirstChild(Teleport).Locked and hit.Parent:IsA("Model") then 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 end local Pos = script.Parent.Parent:findFirstChild(Teleport) hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false end end script.Parent.Touched:connect(Touch)
Use game.Players:GetPlayerFromCharacter(model), it returns nil if no player belongs to that model, so if it isn't nil, you know that's a player.
if hit.Parent:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then --teleport code end
This function is also good to retrieve the player from a character, in case you want to mess with it.
You MUST NOT use game.Players:findFirstChild(Character.Name), because in case someone with the exact same name as an npc enters the game, that specific npc will be able to teleport, for you are not checking if the model belongs to a player, but only if the name of that model is the same of a player.
Edit: I tested the code with these changes and it worked, also changed the cooldown part. If it doesn't work for you maybe the parts you are referencing on your teleporter aren't placed where they should. Be sure that the "te4" part is parented to the same object as the part you touch to activate the code.
local Teleport = "te4" local cooldown = true local cooldowntime = 5 function Touch(hit) if cooldown == true and hit.Parent:IsA("Model") and game.Players:GetPlayerFromCharacter(hit.Parent) then cooldown = false print(game.Players:GetPlayerFromCharacter(hit.Parent).Name) local Pos = script.Parent.Parent:findFirstChild(Teleport) hit.Parent:moveTo(Pos.Position) wait(cooldowntime) cooldown = true end end script.Parent.Touched:connect(Touch) print("Bound Function")