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

I have a teleportation script and it wont work, why?

Asked by 5 years ago
Edited 5 years ago

Hello, I made a teleportation script so when you touch a brick it will "teleport" you to a different place.

This is in LocalScript:

wait(1)
player = game.Players.LocalPlayer
button = script.Parent
local debounce = false

function teleport()
 if not debounce then

 debounce = true
    Torso = player.Character.Torso
    Torso.CFrame = game.Workspace.middletele.CFrame

 end
end

script.Parent.Touched:connect(teleport)

while true do wait()
debounce = false
wait(1)
end

However it wont work.. and I dont know why.. please help me its really important. Thank you.

0
Added a new comment. Zafirua 1348 — 5y

2 answers

Log in to vote
1
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
5 years ago
Edited 5 years ago

There are couple notable errors on your code.

  • The first and foremost being game.Players.LocalPlayer. This is not possible in a Server Script.

  • It also seems you are using a LocalScript. You should not be using in this scenario unless you plan on using Remote Events.

  • The second suggestion is that for this scenario, all of your variables should be localized i.e local

  • You also do not need the while true do loop here. Unless you are using for some other thing, in this case, it is merely pointless.

  • You also should be using HumanoidRootPart because R15 does not support Torso and you could run into an error if you do plan on using R15. If you still would like to use torso, then you will use UpperTorso and LowerTorso.

  • You also should not be using deprecated events. Use Connect() instead of connect().

As suggested by other user, you are going to be using a parameter in the function called hit. Although, the name can be anything.

Here is a fix to your code. Do keep in mind that this is a Server Script.

-- [Declaration Section]
local Part             = script.Parent;
local Debounce         = false;

-- [Processing Section]
local function Teleport_Player (Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then 
        if not Debounce then 
            Debounce   = true;  

            local HRP  = Hit.Parent.HumanoidRootPart;
            HRP.CFrame = CFrame.new(10, 100, 10);

            Debounce   = false;
        end;
    else
        print("Other Object found");
    end;
end;

-- [Connecting Section]
Part.Touched:Connect(Teleport_Player);
0
Could you please explain to me how lines 11 and 12 work? Also I am using R6 in my game since its easier to use when you need to work with only 6 parts instead of 15. So I should write Torso instead of the RootPart? HeyItzDanniee 252 — 5y
0
Humanoid Root Part is the single part that all other character parts are connected. In short, It is better if you use Humanoid Root Part to move characters although it is perfectly fine to also use Torso.  Zafirua 1348 — 5y
0
Oh, ok. Thanks. Also last question why is the CFrame is (10,100,10)? Dont I need to find the CFrame of where do I want to teleport? For example I want to teleports to a specific part so I should use its CFrame? How? HeyItzDanniee 252 — 5y
0
You can do `HRP.CFrame = Part.CFrame` as well. Zafirua 1348 — 5y
Ad
Log in to vote
0
Answered by
T1mes 230 Moderation Voter
5 years ago
Edited 5 years ago

Local scripts have special properties that allow them to get the local player, local player's mouse, etc.

These properties don't work on the player when you don't run it on the player though.

The problem is where the local script is located.

button = script.Parent

I'm guessing because of this line that the local script is a descendant of workspace and cannot run on the player.

I suggest either

  1. Moving the script where it can run on the player (startergui or starterpack) and change
button = workspace.Button -- or where ever/ whatever it's called

or 2. Re-write the script so that it detects anyone who touches it and then teleport them (you would do this by getting the hit parameter through .Touched() by doing:)

function teleport(hit)

Hope this has helped you in anyway!

0
I dont need a button, I want the player to touch a part to teleport. Also the script is in Workspace since its not a GUI. Thanks anyway :P HeyItzDanniee 252 — 5y

Answer this question