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

How do I fix my Teleport system (MessagingService) not working?

Asked by 3 years ago
Edited 3 years ago

I'm trying to make a system where the player can teleport to any server by typing in the username.

This code that I have does not work

Here is the code (made by @OptimisticSide):

local function GetServerData(UserId)
    local placeId, jobId

    PlayerFoundConnection = MessagingService:SubscribeAsync("PlayerFound", function(givenId, givenPlaceId, givenJobId)
        if UserId == givenId then
            placeId = givenPlaceId
            jobId = givenJobId
            PlayerFoundConnection:Disconnect()
        end
    end)

    MessagingService:PublishAsync("GetPlayer", UserId)
    repeat wait() until not PlayerFoundConnection.Connected
    return placeId, jobId
end

MessagingService:SubscribeAsync("GetPlayer", function(UserId)
    for _, player in pairs(game.Players:GetPlayers()) do
        print("UserId: " .. UserId) 
        if player.UserId ==  UserId then
            MessagingService:PublishAsync("PlayerFound", UserId, game.PlaceId, game.JobId)
            return
        end
    end
end)

local function TeleportPlayer(placeId, jobId, player)
    TeleportService:TeleportToPlaceInstance(placeId, jobId, player)
end

game.ReplicatedStorage.TeleportToPlayer.OnServerEvent:Connect(function(username)
    local UserId = game.Players:GetUserIdByNameAsync(username)
    loccal placeid, jobid = GetServerData(UserId)
    TeleportPlayer(placeid, jobid, player)
end)

Output:

UserId: 0x0434518381 -- The userId is a table instead of an integer value

Something is not working here. If anyone know's the answer please tell me. Thanks in advance.

Answer this question