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

How can I make a teleporter not teleport NPCS?

Asked by 3 years ago
Edited 3 years ago

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)

1 answer

Log in to vote
0
Answered by
Dfzoz 489 Moderation Voter
3 years ago
Edited 3 years ago

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")
0
Hmm your solution didnt seem to work and I don't know why. I'm pretty sure I put in the code correctly but nothing. DocGooseYT 110 — 3y
0
I completely forgot to mention this in my comment. When I added this, I wouldn't get teleported anymore and no errors appeared in the console. DocGooseYT 110 — 3y
0
Try adding some prints between lines of your code to know where it stops, like print("1") print("2") and so on, then tell me where it stopped. Dfzoz 489 — 3y
0
Also why do you check if two things are locked? You don't need to check them twice for a cooldown, and you can actually create a value for that, like this: local teleportAvailable == true if teleportAvailable == true then teleportAvailable = false teleport code teleportAvailable = true end Dfzoz 489 — 3y
View all comments (9 more)
0
Weird, I put a print at the beginning and the end. It managed to print both but in the middle of both prints I had a couple more prints that didn't work, and those prints were below "function Touch Hit" I put your code under that too btw. And to answer your question for why I check if two things are locked is because I'm a beginner scripter and I didn't realize that I could do what you just sent. DocGooseYT 110 — 3y
0
Look the edit I made on this post at the end. Dfzoz 489 — 3y
0
Something isn't right.. its still not working. Hold on lemme see if the code I used before I used yours works. if it doesn't something is wrong. DocGooseYT 110 — 3y
0
Alright it works! But.. I get instantly teleported causing in a loop. I keep getting teleported back and forth. It seems like the cooldown code isn't working. DocGooseYT 110 — 3y
0
Here is the model with the parts I used with the code I posted: https://www.roblox.com/library/6376836177/. I created the parts based on what your code was saying, so it should work on yours, unless your parts aren't parented as they were intented to. Dfzoz 489 — 3y
0
Ok, I managed to fix the loop but now the only problem is "te4" getting deleted from my game?? I have no code that does this but there are 3 teleporters that lead to this one teleporter, could it be because of this? DocGooseYT 110 — 3y
0
You can create a BoolValue and paster it inside the model, name it Cooldown, then change the code to check if that value is true instead of the cooldown value created inside the script. Then you can copy that script and paste it inside te4, and inside the cloned script change "te4" to the name of the other teleporter. About the "te4" deletion, It might be another script you have that's doing it. Dfzoz 489 — 3y
0
Also it might be that "te4" isn't an anchored part, so check its properties and toggle te4 to anchored. If it isn't anchored nor set to collide, it will fall through your map and be deleted. Dfzoz 489 — 3y
0
Your right! It isn't anchored?? I'm really confused because I'm not sure how this was unanchored. Huh well your solutions worked so thank you for helping me out! DocGooseYT 110 — 3y
Ad

Answer this question