Hello! thank you so much in advance to anyone who actually reads all this and helps. I’m trying to make a simple tycoon game and right now I’m dealing with datastores. I am really confused.
I have the setting on in studio that lets you access datastore API within studio, so I think my script is the problem. The pcall’s aren’t erroring, because “success” is returning as true every time, but the datastores aren’t being set. This script should only run once since the point of it is to detect if the player has joined before, but it is running fully every time I press test. It seems like all the datastores are returning as Nil when I use GetAsync on any of them.
What am I doing wrong?
local dts = game:GetService("DataStoreService")
-- Player data store initializer
game.Players.PlayerAdded:Connect(function(plr)
local key = plr.UserId
-- Check if player has joined before
local success = false
local results = nil
local count = 0
while not success do
success, results = pcall(function()
dts:GetDataStore("JoinedBefore"):GetAsync(key)
end)
if success then break else
count += 1
warn(plr.Name.." failed has player JoinedBefore, retrying "..tostring(count))
warn(results)
task.wait(0.1)
end
end
if results == nil then -- If player has not joined before
-- Player has not joined before, but now they have, set JoinedBefore to true
success = false
count = 0
while not success do
success, results = pcall(function()
dts:GetDataStore("JoinedBefore"):SetAsync(key, true)
end)
if success then break else
count += 1
warn(plr.Name.." failed player JoinedBefore set to true, retrying "..tostring(count))
warn(results)
task.wait(0.1)
end
end
-- Player has not joined before, initialize data stores
print(plr.Name.." has not joined before, initializing datastores")
success = false
count = 0
while not success do
success, results = pcall(function()
dts:GetDataStore("TycoonUpgrades"):SetAsync(key, {-1})
end)
if success then break else
count += 1
warn(plr.Name.." failed player datastore inizialization, retrying "..tostring(count))
warn(results)
task.wait(0.1)
end
end
success = false
count = 0
while not success do
local success, results = pcall(function()
dts:GetDataStore("Currency"):SetAsync(key, {
Money = 0
})
end)
if success then break else
count += 1
warn(plr.Name.." failed player datastore inizialization, retrying "..tostring(count))
warn(results)
task.wait(0.1)
end
end
count = 0
success = false
while not success do
success, results = pcall(function()
dts:GetDataStore("Stats"):SetAsync(key, {
MinutesPlayed = 0,
TimesJoined = 0
})
end)
if success then break else
count += 1
warn(plr.Name.." failed player datastore inizialization, retrying "..tostring(count))
warn(results)
task.wait(0.1)
end
print(plr.Name.." successfully initialized datastores")
end
end
end)