What do you want to achieve?
For the DataStore to receive the same info the main game sets
What is the issue?
It always returns nil
What solutions have you tried so far?
There’s no documentation stating that it works, so I am not exactly sure.
--Main Game (teleports you) note that this is just shortened to make it simple to read
local DataStoreService = game:GetService("DataStoreService")
local GameInfoDataStore = DataStoreService:GetDataStore("GameInfo")
GameInfoDataStore:SetAsync("TESTINGSERVERRECIEVEINFO", true)
--Part that receives it (also shortened, yes, the function is connected, it prints nil.) local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("GameInfo")
local Data
local Success, ErrorMessage = pcall(function()
Data = DataStore:GetAsync("TESTINGSERVERRECIEVEINFO")
end)
warn(Data) --nil (should be true)
Yes, DataStores do transfer between any place within the same experience.
Before reading, have you tried printing if the pcall was a success or error? You could have easily detected a possible issue.
It could be possible the the game has shutdown before saving data because you got teleported to the other place. You can work around this multiple ways, one is by using game:BindToClose(). This function activates when the server is shutting down, and you can slightly delay it by connecting it to a yielding function.
The Yielding Method
game:BindToClose(function()
task.wait(5)
end)
Note that studio has more yielding capabilities than a normal server would.
You can do more than yielding with BindToClose. If you do not prefer the yield method, you can directly connect a function which calls SetAsync to the datastore.
Teleport Data
You can also use teleport data instead of datastores. Teleport data allows you to send data between servers without need of datastores. It’s useful when you only need to temporarily hold data. Main Game (SERVER)
game:GetService("Players").PlayerAdded:Connect(function(player)
local joinData = player:GetJoinData()
local capturedData = joinData.TeleportData
local Data = capturedData.TESTINGSERVERRECIEVEINFO
print(Data) --> true
end)
Note that you don’t have to use a table only for teleport data, you can use most types of data.
Example:
Never mind. I accidently tried to concatenate a string with ErrorMessage, which was nil, and since it happened before it printed the data, it never printed true. Thank you.