Hello. In my game, i require 2 datastores. One which holds the players win’s and coins and the other for abilities in the background, something is not right though as these datastores are heavily broken. For datastore number one (The one with wins and coins) is delayed by 5 seconds or so, could somebody help me why it does that?
For the 2nd one, it won’t save the 2nd or 3rd boolvalue half of the time. Please help!
datastore one
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
local ds2 = DataStore:GetDataStore("LeaderStatSave2")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("NumberValue",leader)
Cash.Name = "Wins"
local Cash2 = Instance.new("NumberValue",leader)
Cash2.Name = "Coins"
Cash.Value = ds:GetAsync(player.UserId) or 0
Cash2.Value = ds2:GetAsync(player.UserId) or 0
ds:SetAsync(player.UserId, Cash.Value)
ds2:SetAsync(player.UserId, Cash2.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
Cash2.Changed:connect(function()
ds2:SetAsync(player.UserId, Cash2.Value)
end)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Wins.Value)
ds2:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
Datastore two
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Ability")
local ds2 = DataStore:GetDataStore("Ability2")
local ds3 = DataStore:GetDataStore("Ability3")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "Abilities"
local Cash = Instance.new("BoolValue",leader)
Cash.Name = "Boost"
local Cash2 = Instance.new("BoolValue",leader)
Cash2.Name = "Heal"
local Cash3 = Instance.new("BoolValue",leader)
Cash3.Name = "Engineer"
Cash.Value = ds:GetAsync(player.UserId) or false
Cash2.Value = ds2:GetAsync(player.UserId) or false
Cash3.Value = ds3:GetAsync(player.UserId) or false
ds:SetAsync(player.UserId, Cash.Value)
ds2:SetAsync(player.UserId, Cash2.Value)
ds3:SetAsync(player.UserId, Cash3.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
Cash2.Changed:connect(function()
ds2:SetAsync(player.UserId, Cash2.Value)
Cash3.Changed:connect(function()
ds2:SetAsync(player.UserId, Cash3.Value)
end)
end)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.Abilities.Boost.Value)
ds2:SetAsync(player.UserId, player.Abilities.Heal.Value)
ds3:SetAsync(player.UserId, player.Abilities.Engineer.Value)
end)
well the way you’re using datastores is very weird so I’d just add a bunch of debug statements. Also please don’t reply to me after I haven’t replied in like. 8 minutes lol
Are you sure you need 3 data stores for storing 3 values?
Also do not set async data when value is changed, if it does too frequently then it will fail. Instead store it in a table and save the table to one data store when player leaves
game.Players.PlayerAdded:Connect(function(plr)
temporalData[plr.UserId] = ds:GetAsync(plr.UserId)
--make all your bool values here
local cash1 = temporalData[plr.UserId].Cash1
if cash1 == nil then
cash1 = false
end
Cash.Value = cash1
Cash.Changed:connect(function()
temporalData[plr.UserId].Cash1 = Cash.Value
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
--Save it to one data store
ds:SetAsync(player.UserId, temporalData[plr.UserId])
temporalData[plr.UserId] = nil
end)
Oh, these are tables. Oh… i have lots of questions about theses…
First, what’s the top script aka the TemporalData for?
Second, How would i exactly make all my bool values, the same as before or?
Third, What do you mean by “Save it to one datastore”
Lastly, Can i repeat this for my ability datastore?
Thank you.
This part adds player data to table when player is added, so “top script” temporalData is required:
temporalData[plr.UserId] = ds:GetAsync(plr.UserId)
--to make the values same as before set them to loaded data
--whoops
local cash1 = temporalData[plr.UserId].Cash1
--if there's none of whatever you need then set it to false (or 0 if it's NumberValue)
if cash1 == nil then
cash1 = false
end
Cash.Value = cash1
when player leaves, it will save the table to data store:
--just have one data store like: DataStore:GetDataStore("PlayerData")
ds:SetAsync(player.UserId, temporalData[plr.UserId])
--and set it to nil to save memory
temporalData[plr.UserId] = nil
temporalData[plr.UserId] = ds:GetAsync(plr.UserId)
temporalData[plr.UserId].Wins = 0
--to make the values same as before set them to loaded data
--whoops
local cash1 = temporalData[plr.UserId].Cash1
local win1 = temporalData[plr.UserId].Wins1
--if there's none of whatever you need then set it to false (or 0 if it's NumberValue)
if cash1 == nil then
cash1 = false
end
Cash.Value = cash1