Search code examples
luaroblox

Transporting player from different places via touching a block


I need help transporting player from different places via touching a block. As in to teleport player to another PLACE, not location, within the experience. Whats meant to happen is the player to touch a part and then to be teleported to a place.

local TeleportService = game:GetService("TeleportService")
local Part = script.Parent
local TARGET_PLACE_ID = 12108199305 -- replace with your own place ID

local playerToTeleport = Players:GetPlayers()[1] -- get the first user in the experience


local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

Part.Touched:Connect(function(Object)
    if Object.Parent:FindFirstChild("Humanoid") then
        
        TeleportService:TeleportAsync(TARGET_PLACE_ID, game.Players[Object.Parent.Name])
    end
end)

I get this error: 17:20:06.603 Unable to cast value to Objects - Server - Script:15


Solution

  • Try this :

    
    local TeleportService = game:GetService("TeleportService")
    local Part = script.Parent
    local TARGET_PLACE_ID = 12108199305 -- replace with your own place ID
    
    
    local Players = game:GetService("Players")
    
    
    Part.Touched:Connect(function(Object)
        if Object and Object.Parent then
            local PlayerName = Object.Parent.Name
            if Object.Parent:FindFirstChild("Humanoid") and Players:FindFirstChild(PlayerName) then
                TeleportService:TeleportAsync(TARGET_PLACE_ID, Players[PlayerName])
            end
        end
    end)
    
    

    Note: Put the code in server sided script in the part instead of a local script!

    Let me know if it works!