I’m making a DataStore for my obby that I am working on and when I was testing it and working in Roblox Studios, the script worked fine and no errors were happening. The problem is whenever I join my game through Roblox the DataStore doesn’t seem to work. Here is my script:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(plr.userId)
end)
if success then
Level.Value = data
else
print("There was an error with getting "..plr.Name.. " data!")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i, player in ipairs(game.Players:GetPlayers()) do
if i >= 2 then
local success, errormessage = pcall(function()
myDataStore:SetAsync(plr.userId, plr.leaderstats.Level.Value)
print("Saving "..plr.Name.." data!")
end)
if success then
print(plr.Name.." data was saved!")
else
print("There was an error saving data.")
end
end
end
end)
game:BindToClose(function()
for i, player in ipairs(game.Players:GetPlayers()) do
if i == 1 then
local function save(plr: Player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(plr.userId, plr.leaderstats.Level.Value)
print("Saving "..plr.Name.." data!")
end)
if success then
print(plr.Name.." data was saved!")
else
print("There was an error saving data.")
end
end
save(player)
end
end
end)