local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("MyDataStore") game.Players.PlayerAdded:connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent= player local stage = Instance.new("IntValue") stage.Name = "Stage" stage.Parent leaderstats ack2.Value = 0 local data local success, errormessage = pcall(function) myDataStore:GetAsync(player.UserId.."") end) if success then stage.value = data else print("There was an error whilst getting your data") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function) myDataStore:SetAsync(player.UserId.."stage",player.leaderstats.Stage.Value) end) if success then print("Player Data successufully saved!") else print("There was an error when saving data") warn(errormessage) end end) stage.Value = ds1:GetAsync(plr.UserId) or 1 ds1:SetAsync(plr.UserId, stage.Value) stage.Changed:connect(function() ds1:SetAsync(plr.UserId, stage.Value) end) end)
Your program contained syntax errors on lines 16-18. You also had several logic issues with you Script which were going to run you into a cesspool of semantic errors too. Please be careful when writing code.
It is also highly recommended not to to make :SetAsync()
calls every time the ValueObject updates. You put your DataStore at high-risk for exhaustion problems. Since you're saving a single Value, it's not important to do so either, so just leave requests for PlayerLeaving
.
You should also use :BindToClose()
with DataStore's to compensate for abrupt server-shutdownts.
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("MyDataStore") local Players = game:GetService("Players") Players.PlayerAdded:connect(function(Player) local Leaderstats = Instance.new("Folder") Leaderstats.Name = "leaderstats" Leaderstats.Parent= Player local Stage = Instance.new("IntValue") Stage.Name = "Stage" Stage.Value = 0 Stage.Parent = leaderstats local Data; local Success, ErrorMessage = pcall(function() Data = myDataStore:GetAsync(Player.UserId.."-Stage") end) if (Success) then Stage.Value = Data else print("There was an error whilst getting your data!") warn(ErrorMessage) end end) Players.PlayerRemoving:Connect(function(Player) local Leaderstats = Player:FindFirstChild("Leaderstats") if (Leaderstats and Leaderstats.Stage) then local Stage = Leaderstats.Stage.Value local Success, ErrorMessage = pcall(function() myDataStore:SetAsync(Player.UserId.."-Stage", Stage) end) if (Success) then print("Successfully saved data!") else print("There was an error whilst saving your data!") warn(ErrorMessage) end end end) game:BindToClose(function() for _,Player in pairs(Players:GetPlayers()) do Player:Kick() --// Force PlayerLeaving to fire end end)
Remember to accept this answer if it helps!