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

Teleportation script won't teleport a player?

Asked by 7 years ago
Edited 7 years ago

So, I made a script to where if a player touches a block, it will teleport them to the position I want them to teleport to. Problem is, when I did this, the player won't teleport. I am a complete noob at scripting with lua so I make seeming simple mistakes often.

local teleport = script.Parent
local playerCFrame = game.Workspace.Player.HumanoidRootPart.CFrame

local function playerHit(part)
    local parent = part.Parent
    if game.Players:GetPlayerFromCharacter(parent) then
        playerCFrame = CFrame.new(Vector3.new(-50, 12, -973.5))
    end
end

teleport.Touched:connect(playerHit)

EDIT: Player might be a placeholder name

0
If player is a placeholder name, I could make it a variable to find the player's username. IamTewsmrt 6 — 7y

2 answers

Log in to vote
0
Answered by
halwa 11
7 years ago

Hey, This is just simple. You can make a part named location in workspace that the player is moved to when he touches this script's Parent. Put this in a server script. The script's Parent should be the part you have to touch to teleport.

local Part = script.Parent
local Location = game.Workspace.location.Position
Part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local Character = hit.Parent
        Character:MoveTo(location)
    end
end)

~~ Halwa

Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Hey IamTewsmrt,

The problem with your script that I first noticed is that you set a variable for the CFrame of the HumanoidRootPart. So, when you set the variable equal to something, it won't change anything about the HumanoidRootPart, it will just change the value that it is holding which is simply the HumanoidRootPart's CFrame. So, below is a good example of what I'm talking about.

Example A

local cframe = workspace.Part.CFrame; -- Pretend the CFrame value is: 0, 0, 0.
local other_part = workspace.OtherPart; -- Pretend the CFrame of this part is: 5, 5, 5.

cframe = other_part.CFrame;

-- After that all you have done is set: CFrame.new(0, 0, 0) to CFrame.new(5, 5, 5);

As you can see from the above example you have just set the variable equal to something, you haven't mutated the Part itself. If you want to mutate the HumanoidRootPart's properties itself then you need to set a variable for the HumanoidRootPart and simply set it's CFrame equal to the playerCFrame. Also, you need to use the :WaitForChild() method because it takes time for the Player's Character to load. Below is a personal example of what you need to do to make this work.

Example B

local part = script.Parent;
local root = workspace:WaitForChild("Player1").HumanoidRootPart;
local cframe = root.CFrame;

function touched(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid");
    if hum then
        cframe = CFrame.new(Vector3.new(-50, 12, -973.5));
        root.CFrame = cframe;
    end
end

part.Touched:Connect(touched);

-- EDIT:

So, apparently it won't work in the game for you. Well, the simple answer to that is because in the game your character isn't named "Player1" it's your actual name and you are trying to get the Character "Player1", which doesn't exist in the game. So, I recommend defining the root and cframe inside of the function after you check if the humanoid exists. However, don't define the root as:

local root = workspace:WaitForChild("Player1").HumanoidRootPart;

rather, define it as:

local root = hit.Parent:WaitForChild("HumanoidRootPart");

Also, I hope you're using a ServerScript(Normal Script, not a Local Script) for this operation. Well, I hope I helped in one way or another and I hope you have a nice day/night.

~~ KingLoneCat

0
It works while it is still in studio, but when I want to do it in the game, it won't teleport. IamTewsmrt 6 — 7y
0
That is because in the game your name isn't "Player1" It's your own name... KingLoneCat 2642 — 7y

Answer this question