Making a teleport script server sided?

So, I’ve been trying to edit this script to work with a part and to move the HumanoidRootPart via CFrame to a part and then making sure the camera faces the right way. Is there a way I can make this script server sided and fix it? Works if changed a little to work with a ScreenGUI and textbutton but doesn’t with a part.
Mostly, been having problems getting the character’s HumanoidRootPart and CFraming it to another part while changing the camera using a server sided script. If theres a way to do this without a server sided script and using a local script, it would also help.

Script:

Player = game:GetService("Players")
Dest = script.Parent.Destination
Teleporter = script.Parent.Teleporter
HasPressedTeleport = false
local Cam = workspace.CurrentCamera


Teleporter.Touched:connect(function(part)
	 if HasPressedTeleport == false then
        local Character = Player.Character or Player.CharacterAdded:Wait()
local Character = part.Parent

    if Character then
        local HRP = Character:WaitForChild('HumanoidRootPart')
        HRP.CFrame = Dest.CFrame

        repeat
            wait()
            Cam.CameraType = Enum.CameraType.Attach
        until Cam.CameraType == Enum.CameraType.Attach

        repeat
            wait()
            Cam.CameraType = Enum.CameraType.Custom
        until Cam.CameraType == Enum.CameraType.Custom
    end     
    end
end)

Teleporter.Touched:connect(teleport)

Explorer:
image

Is there any reason you can’t use a LocalScript? You’re just moving the player and doing camera manipulation, right?

Do local scripts even work in workspace? Or should I use a local script to detect if the character touches a part named Teleporter to go to the Destination?

1 Like

No, LocalScripts don’t work in the workspace, but you can either check when the character’s touched like you said or reference the part that is in workspace from somewhere else. Just so you know, the wiki says where LocalScripts can run.

How would I detect what part the character touches using a local script?

Edit: Would I just use the touched event as normal?

Yep, but you’d have to loop through each of the character’s parts and connect a function to all of their touched events. For example:

local function touchedThing(part)
     -- touched event stuff here
end
for _, child in pairs(character:GetChildren()) do -- character would be defined somewhere else
     if child:IsA("BasePart") then
          child.Touched:Connect(touchedThing)
     end
end
1 Like

Couldn’t I just do it for HumanoidRootPart since that’s what im moving to move the entire character?
(I’m using using R6 btw)

Not unless you’re using some kind of custom character; the default avatar touches stuff with its legs. You’d only have to set the CFrame of the HumanoidRootPart to teleport the character though.

1 Like

They can use Humanoid.Touched, which will fire if any part of the character touches something. Much easier and more reliable than looping through the entire characters parts.

As for the main question, I would just use a LocalScript, located somewhere that it can be ran (StarterGui is fine, but I’d personally put it in StarterPlayerScripts)

2 Likes

Correct me If i’m wrong but isnt it bad to use that since:


Its fires whenever the humanoid is moving or touching something?

Edit: How would I make it fire instead when It touches a certain part instead of looping it till it hits the part?

Just check that it’s touching what you care about, it works the same way as a normal Touched event.

1 Like

It would yes, but you can just check to make sure it only continues if the part it is touching is your part.

1 Like

Thanks for the help @posatta and @NinjoOnline. Finally got it working correctly!

1 Like

You could improve on this method by using CollectionService to tag the teleported with (for example) a teleporter tag, and check if the basePart which was touched has the tag before teleporting.