Hello! My datastores stopped working today, today I didn't do anything. I don't know what caused the error. Here is the datastore script:
local datastore = game:GetService("DataStoreService") local data1 = datastore:GetDataStore("TapsData112") local data2 = datastore:GetDataStore("CoinsData113") local data3 = datastore:GetDataStore("dimension1Data112") --powerup data local powerUpsStore = datastore:GetDataStore("POWERUP_DATA") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local Clicks = Instance.new("IntValue", leaderstats) Clicks.Name = "Clicks" local Coins = Instance.new("IntValue", leaderstats) Coins.Name = "Coins" local hasTrail = Instance.new("IntValue", player) hasTrail.Name = "hasTrail" hasTrail.Value = 0 local hasDimension1 = Instance.new("IntValue", player) hasDimension1.Name = "hasDimension1" --powerup stuff below local powerUp1 = Instance.new("IntValue", player) powerUp1.Name = "powerUp1" local powerUp2 = Instance.new("IntValue", player) powerUp2.Name = "powerUp2" local powerUpData = powerUpsStore:GetAsync(player.UserId) or {0, 0} powerUp1.Value = powerUpData[1] powerUp2.Value = powerUpData[2] game.Players.PlayerRemoving:Connect(function(player) data1:SetAsync(player.UserId, Clicks.Value) data2:SetAsync(player.UserId, Coins.Value) data3:SetAsync(player.UserId, hasDimension1.Value) powerUpsStore:SetAsync(player.UserId, { powerUp1.Value, powerUp2.Value }) end) Clicks.Value = data1:GetAsync(player.UserId) or 0 data1:SetAsync(player.UserId, Clicks.Value) Coins.Value = data2:GetAsync(player.UserId) or 0 data2:SetAsync(player.UserId, Coins.Value) hasDimension1.Value = data3:GetAsync(player.UserId) or 0 data3:SetAsync(player.UserId, hasDimension1.Value) end)
I'm planning to release my game a few days before Christmas, so I really need to fix it. Also it's not working in any of my game's previous versions.
Start by copy and pasting this into your script, I will explain once you're done:
local StoreService = game:GetService("DataStoreService") local DataStore = StoreService:GetDataStore("your_data_store_here") local function PlayerJoined(User) local leaderstats = Instance.new("Folder") -- it's bad practise to set parents in the brackets. leaderstats.Name = "leaderstats" leaderstats.Parent = User local Value1 = Instance.new("IntValue") -- don't call it value1, it's just an example. Value1.Name = "Value1" Value1.Value = 0 Value1.Parent = leaderstats local Value2 = Instance.new("IntValue") Value2.Name = "Value2" Value2.Value = 0 Value2.Parent = leaderstats local UserId = "User-"..User.UserId local data = DataStore:GetAsync(UserId) if data then Value1.Value = data["Value1"] Value2.Value = data["Value2"] else Value1.Value, Value2.Value = 0, 0 end end
Essentially what's going on here is I make a function which holds values just like this. You SHOULD know how userids and getting data works..GetAsync..
Now, look below and copy it (make a new 2-3 lines before doing so):
local function create_stat_table(User) -- We want to get the children of leaderstats. local user_stats = {} for _, stat in pairs(User.leaderstats:GetChildren()) do -- for everything in leaderstats, do: user_stats(stat.Name) = stat.Value end return user_stats -- returning the values will help when we call the function. end
The PAIRS statement just finds any children of "leaderstats", then if it does it will add THAT child into the user_stats_ table:
--[[ when you do "user_stats(stat.Name) = stat.Value, it would look like this: Computer: "....Oh, so I add a new variable with a value!:" user_stats(stat = value) !!STAT AND VALUES ARE JUST EXAMPLES, stat would be the name of the stat, and the value would be the value of the stat!! ]]--
Now, let's do the player removing function (same as last time, 2-3 new lines):
local function PlayerLeft(User) local user_stats = create_stat_table(User) -- the returned values will replace the function. local success, err = pcall(function() local UserId = "User-"..User.UserId local data = DataStore:SetAsync(User.UserId, user_stats) -- you set the values with the table. end if not success then print(err) -- print what error has happened if it fails end
The pcall functon is just for error handling; you can check if the code within this code block errors or not.
GetAsync: Fetching values, good for checking if they have existing data or not. SetAsync: Changing values to whatever we want, in this case we want it to change is maybe Coins, etc.
FINALLY, make 2-3 more lines again and do:
game.Players.PlayerAdded:Connect(PlayerJoined) -- connecting function to event game.Player.PlayerRemoving:Connect(PlayerLeft) -- no need to call table function here, we've already called it.
I hope this clears up what was wrong, if not please reply and I'll try my best to explain! :3
P.S don't always use functions for one-time-use events, such as a player joined event. it's not very good practise..