My datastore in roblox isn't working, I have made multiple scripts one after another for leaderstats saving and it only works sometimes. Sometimes it works and other times it doesn't. I am a beginner but I do know how to script. My datastores always fail and you end up back at 0 at the end. I need help and here are the scripts:
Script 1: local ds = game:GetService("DataStoreService") local store = ds:GetDataStore("VibeTokens")
game.Players.PlayerAdded:connect(function(plr) local f = script.Folder:Clone(); f.Parent = plr; --Give them the stats local potential = store:GetAsync(plr.userId.."_stats");
for i,v in next, f:GetChildren() do
f[v.Name].Value = (potential ~= nil and potential[v.Name]) or false;
end
end)
function saveStats(plr) local f = plr:FindFirstChild("Folder"); local data = {}; for _,v in next,f:GetChildren() do data[v.Name] = v.Value; end
store:SetAsync(plr.userId.."_stats",data);
end
game.Players.PlayerRemoving:connect(function(plr) saveStats(plr)
game.OnClose = function() for _,v in next,game.Players:GetPlayers() do saveStats(v); end end
Script 2: local DataStoreService = game:GetService('DataStoreService') local myDataStore = DataStoreService:GetDataStore('PlayerData')
local function onPlayerJoin(player) local leaderstats = Instance.new('Folder') leaderstats.Name = 'leaderstats' leaderstats.Parent = player
local vibetokens = Instance.new('IntValue')
vibetokens.Name = 'VibeTokens'
vibetokens.Parent = leaderstats
local playerUserId = 'Player_'..player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
vibetokens.Value = data
else
vibetokens.Value = 0
end
end
local function onPlayerExit(player) local success, err = pcall(function() local playerUserId = 'Player_'..player.UserId playerData:SetAsync(playerUserId, player.leaderstats.VibeTokens.Value) end) if not success then warn('Could not save data') end end
game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)
Script 3: local DataStore = game:GetService("DataStoreService"):GetDataStore("VibeTokensDataStore")
function onPlayed(player)
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = player
local vibetokens = Instance.new("IntValue")
vibetokens.Name = "VibeTokens"
vibetokens.Parent = ls
local ID = "vibetokens".."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
vibetokens.Value = savedData
print("[VIBETOKENS DATASTORE] "..player.Name.."'s data has been loaded.")
else
vibetokens.Value = 0
print("[] No data detected ... ")
end
end
function onRemoved(player)
local ID = "vibetokens".."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats.VibeTokens.Value)
end
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("[KICKED] Reason: Kicked to prevent data loss.")
end
end
wait(5)
end)
game.Players.PlayerAdded:Connect(onPlayed) game.Players.PlayerRemoving:Connect(onRemoved) RAW Paste Data local DataStore = game:GetService("DataStoreService"):GetDataStore("ValueDataStore")
function onPlayed(player)
local ID = "".."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
value.Value = savedData
print("[] "..player.Name.."'s data has been loaded.")
else
value.Value = 0
print("[] No data detected ... ")
end
end
function onRemoved(player)
local ID = "".."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats.value.Value)
end
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("[KICKED] Reason: Kicked to prevent data loss.")
end
end
wait(5)
end)
game.Players.PlayerAdded:Connect(onPlayed) game.Players.PlayerRemoving:Connect(onRemoved) Public Pastes My Log File HTML 5 | 8 min ago Untitled JavaScript | 13 min ago Untitled JavaScript | 21 min ago My Log File HTML 5 | 23 min ago My Log File HTML 5 | 23 min ago shutdown Lua | 27 min ago TempAndHumdity C++ | 29 min ago main.cpp C++ | 34 min ago
Script 4: local DataStoreService = game:GetService("DataStoreService") local DataVault = DataStoreService:GetDataStore("DataVault")
game.Players.PlayerAdded:Connect(function(Player)
Stat = Player:FindFirstChild("leaderstats").VibeTokens
Key = "Key-"..Player.UserId
local GetSaved = DataVault:GetAsync(Key)
if GetSaved then
Stat.Value = GetSaved[1]
else
local SavingNumbers = {Stat.Value}
DataVault:SetAsync(Key, SavingNumbers)
end
print(Player.Name.." has their data loaded (uMicah's Save Script)")
end)
game.Players.PlayerRemoving:Connect(function(Player) local SaveTable = {Stat.Value} DataVault:SetAsync(Key, SaveTable) end)

2 comments
Please use code blocks correctly.
Please use code blocks correctly.