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

Teleporter Keeps Teleporting All Players?? (PLS HELP)

Asked by 6 years ago
Edited 6 years ago

So I Made A LocalScript And Put It In StarterPlayer > StarterCharacterScripts.

Here Is My Script:

local Teleporter1 = workspace.Teleporter1 local Teleporter2 = workspace.Teleporter2 local TeleporterTouched = false

Teleporter1.Touched:connect(function(hit) if game.Players.LocalPlayer then

    local HumanoidRootPart = script.Parent:FindFirstChild("HumanoidRootPart")
    local Torso = script.Parent:FindFirstChild("Torso")

    if hit.Name == "HumanoidRootPart" or hit.Name == "Torso" or hit.Name == "Handle" then
        if TeleporterTouched == false then

            print(hit.Name.." Touched Teleporter1")

            TeleporterTouched = true
            print(TeleporterTouched)

            if HumanoidRootPart then

                print("HumanoidRootPart")
                HumanoidRootPart.CFrame = CFrame.new(15.381, 3, 0.12)  -- Change The Position To Teleporter2 Position

            end

            if Torso then

                print("Torso")
                Torso.CFrame = CFrame.new(15.381, 3, 0.12)  -- Change The Position To Teleporter2 Position

            end

            wait(3)

            TeleporterTouched = false
            print(TeleporterTouched)

        end 
    end
end

end)

So I Added Two Parts Named "Teleporter1" & "Teleporter2" Into Workspace. When A Player Touches "Teleporter1", It Will Teleport ONE Player To "Teleporter2".

But I Don't Know Why Even After I Added Debounce It Still Teleports All Players. Ive Tested It In StarterServer(2 Players), Im Not Sure About The Actual Game But I Think If Theres Error In StarterServer, There Should Be Error In Actual Game As Well.

Any Idea?? Any Helps And Answers Will Be Greatly Appreciated.

2 answers

Log in to vote
0
Answered by
oilsauce 196
6 years ago

I'm not completely sure if this is the problem, but I assume the script you posted is a LocalScript and is parented to your character.

The script you posted is supposed to run anything under Teleporter1.Touched:Connect(function(hit) when Teleporter1 is touched.

The problem is, the script is inside of every character in the game. If anything touches Teleporter1, and game.Players.LocalPlayer is true, the Touched event will fire for every character because every character has the LocalScript to do so.

In other words, it's not Teleporter1 that's going to teleport you, but it's your own character with the LocalScript.

Your Script:

local Teleporter1 = workspace.Teleporter1
local Teleporter2 = workspace.Teleporter2
local TeleporterTouched = false

Teleporter1.Touched:connect(function(hit)
if game.Players.LocalPlayer then
    local HumanoidRootPart = script.Parent:FindFirstChild("HumanoidRootPart")
    local Torso = script.Parent:FindFirstChild("Torso")

    if hit.Name == "HumanoidRootPart" or hit.Name == "Torso" or hit.Name == "Handle" then
        if TeleporterTouched == false then

            print(hit.Name.." Touched Teleporter1")

            TeleporterTouched = true
            print(TeleporterTouched)

            if HumanoidRootPart then

                print("HumanoidRootPart")
                HumanoidRootPart.CFrame = CFrame.new(15.381, 3, 0.12)  -- Change The Position To Teleporter2 Position

            end

            if Torso then

                print("Torso")
                Torso.CFrame = CFrame.new(15.381, 3, 0.12)  -- Change The Position To Teleporter2 Position

            end

            wait(3)

            TeleporterTouched = false
            print(TeleporterTouched)

        end 
    end
end
end)

To fix this, we make a script inside of Teleporter1, and make it teleport any character that touches it.

How do we do that? First, we put a Script (not a LocalScript) inside of Teleporter1. Why a Script instead of LocalScript? Because LocalScripts are only ran by a client. We want the server to make characters teleport, so we use a Script.

You've already written most of the script, so all we need to do is change some variables so it won't error when changed to a Script or when parented into Teleporter1 instead of a character.

-- Remember, this is parented inside of Teleporter1, and is a Script.

local Teleporter1 = workspace.Teleporter1 -- or script.Parent, since Teleporter1 is the parent of this script.
local Teleporter2 = workspace.Teleporter2
local TeleporterTouched = false

Teleporter1.Touched:Connect(function(hit) -- use :Connect() instead of :connect(), since it's deprecated.
    if hit.Parent:FindFirstChild("Humanoid") then -- if the parent of the part that touched the teleporter has a Humanoid inside of it, it's most likely a character.
        local theCharacter = hit.Parent -- hit.Parent would be the character that touched the teleporter.
        local HumanoidRootPart = theCharacter:FindFirstChild("HumanoidRootPart")
        local Torso = theCharacter:FindFirstChild("Torso")
            if TeleporterTouched == false then

                print(theCharacter.Name.." Touched Teleporter1")

                TeleporterTouched = true
                print(TeleporterTouched)

                if HumanoidRootPart then

                    print("HumanoidRootPart")
                    HumanoidRootPart.CFrame = CFrame.new(15.381, 3, 0.12)  -- Change The Position To Teleporter2 Position

                end

                if Torso then

                    print("Torso")
                    Torso.CFrame = CFrame.new(15.381, 3, 0.12)  -- Change The Position To Teleporter2 Position

                end

                wait(3)

                TeleporterTouched = false
                print(TeleporterTouched)

            end
    end
end)

Hope this helped, I'll try to answer any questions that are asked if I'm online.

Here are some useful links that might help you out:

https://developer.roblox.com/api-reference/event/BasePart/Touched https://developer.roblox.com/api-reference/class/LocalScript https://developer.roblox.com/api-reference/class/Script

Ad
Log in to vote
0
Answered by 6 years ago

@oilsause

Thanks man problem solved

Idk why but ive put a script inside of Teleporter1 before but it doesnt work. I was trying to find out what can i do so that a player touches the part it wont teleport all the player, until i saw ur script.

So all im lacking was:

if hit.Parent:FindFirstChild("Humanoid") then

and

Deleting:

if hit.Name == "HumanoidRootPart" or hit.Name == "Torso" or hit.Name == "Handle" then

Thanks dude!

Answer this question