How do I access a ReservedServerAccessCode with Datastore

I want to be able to access a datastore that saves the ReservedServerAccessCode so that one of my moderators can join the game if a player calls for help in my moderation system.

This is how I save it;

for _, player in pairs(PlayersToTeleport) do
pcall(function()
DataStores:SetAsync(player.UserId,DataCodeFromTeleportService)
end)
end

I try to access it like this:

local joindatacode
	pcall(function()
	joindatacode = dataStores:GetAsync(v.CreatorId)
	end)
	if joindatacode ~= nil then
		tpservice:TeleportToPrivateServer(v.PlaceId, joindatacode, player)
	end

I keep getting an error that i’m unable to cast this value, any ideas?

1 Like

Which specific code is presenting an error, the save or the load code? What is the exact error you’re getting in the console? Detailed explanations of the issues you’re receiving, as well as any additional information you think would help attract a solution, would be much appreciated.

Ah my mistake, forgot to mention which part, the join data returns this error;

tpservice:TeleportToPrivateServer(v.PlaceId, joindatacode, player)

1 Like

The third argument of TeleportToPrivateServer is meant to be an array of objects, not a single player. To fix this issue, wrap the player object in a single-entry table.

tpservice:TeleportToPrivateServer(v.PlaceId, joindatacode, {player})

A note as well; you may want to do some error handling regarding joindatacode in case DataStore endpoints are down or experiencing an error. You currently only check if the return value is nil but you’ll also need to account for errors. That could affect the functionality of TeleportToPrivateServer by way of attempting to teleport to an invalid instance. I got you on that one.

local success, joindatacode = pcall(dataStores.GetAsync, dataStores, v.CreatorId)
if success and joindatacode then
    tpservice:TeleportToPrivateServer(v.PlaceId, joindatacode, {player})
else
    warn(joindatacode) -- joindatacode replaced with message of error
end
3 Likes

This fixed it, as well as caught an another error that I didn’t even know I made! Thank you for also giving me this piece of code that fixed the casting issue, as well as checking for any errors in the datastore!