Can anyone tell me why this DataStore is not working..
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Hello") game.Players.PlayerAdded:Connect(function(plr) local uniquekey = "id-"..plr.userId local leaderstats = Instance.new("IntValue",plr) local savevalue = Instance.new("IntValue") local savevalue2 = Instance.new("IntValue") local savevalue3 = Instance.new("IntValue") leaderstats.Name = "leaderstats" savevalue.Parent = leaderstats savevalue.Name = "Money" savevalue2.Parent = leaderstats savevalue2.Name = "Subscribers" savevalue3.Parent = leaderstats savevalue3.Name = "Followers" local GetSaved = DataStoreService:GetAsync(uniquekey) if GetSaved then savevalue.Value = GetSaved[1] savevalue2.Value = GetSaved[2] savevalue3.Value = GetSaved[3] else local NumbersForSaving = {savevalue.Value, savevalue2.Value, savevalue3.Value} DataStoreService:SetAsync(uniquekey, NumbersForSaving) end end) game.Players.PlayerRemoving:connect(function(plr) local uniquekey = "id-"..plr.userId local Savetable = {plr.leaderstats.Cash.Value, plr.leaderstats.Subscribers.Value, plr.leaderstats.Followers.Value} DataStoreService:SetAsync(uniquekey, Savetable) end)
The problem is that you are calling SetAsync in the else statement in the playeradded event. You need to realise that that method yields the script until the data is found and considering that your providing the else statement with the fact it has no data to save; it will yield it forever. Here is the fix:
--MAKE SURE IT IS A SERVERSCRIPT IN SERVERSCRIPTSERVICE local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Hello") game.Players.PlayerAdded:Connect(function(plr) local uniquekey = "id-"..plr.userId local leaderstats = Instance.new("IntValue",plr) local savevalue = Instance.new("IntValue") local savevalue2 = Instance.new("IntValue") local savevalue3 = Instance.new("IntValue") leaderstats.Name = "leaderstats" savevalue.Parent = leaderstats savevalue.Name = "Money" savevalue2.Parent = leaderstats savevalue2.Name = "Subscribers" savevalue3.Parent = leaderstats savevalue3.Name = "Followers" local GetSaved = DataStoreService:GetAsync(uniquekey) or {} --Checks if there is data to load if GetSaved then savevalue.Value = GetSaved[1] or 0 --If there is no data then just give it a default of 0. savevalue2.Value = GetSaved[2] or 0 --If there is no data then just give it a default of 0. savevalue3.Value = GetSaved[3] or 0 --If there is no data then just give it a default of 0. --Since we have already certified any errors appearing if the data is nil, there is no need for the else statement. end end) game.Players.PlayerRemoving:connect(function(plr) local uniquekey = "id-"..plr.userId local Savetable = {plr.leaderstats.Cash.Value, plr.leaderstats.Subscribers.Value, plr.leaderstats.Followers.Value} DataStoreService:SetAsync(uniquekey, Savetable) end)