I'm making a datastore script that is gonna save multiple values later on so that's why I set it up like this.
Code:
local datastore = game:GetService("DataStoreService") local stat_save = datastore:GetDataStore("Stat_save") function OnAdded(player) local prefix = "Player_"..player.UserId local data local playerstats = Instance.new("Folder",player) local Coins = Instance.new("IntValue",playerstats) local sucess, err = pcall(function() data = stat_save:GetAsync(prefix) end) if sucess then if data then Coins.Value = data[1] print(data[1]) print("data found") elseif not data then print("no data") elseif not sucess then warn("Error occured: "..err) end end end game.Players.PlayerAdded:Connect(OnAdded) function OnLeave(player) local prefix = "Player_"..player.UserId local sucess, err = pcall(function() stat_save:SetAsync(prefix, stat_save[1]) print("saved in datastore: "..stat_save[1]) end) end game.Players.PlayerRemoving:Connect(OnLeave) local datastore = game:GetService("DataStoreService") local stat_save = datastore:GetDataStore("Stat_save") function OnAdded(player) local prefix = "Player_"..player.UserId local data local playerstats = Instance.new("Folder",player) local Coins = Instance.new("IntValue",playerstats) local sucess, err = pcall(function() data = stat_save:GetAsync(prefix) end) if sucess then if data then Coins.Value = data[1] --getting the first data in the datastore and assigning it print(data[1]) print("data found") elseif not data then print("no data") --prints this elseif not sucess then warn("Error occured: "..err) end end end game.Players.PlayerAdded:Connect(OnAdded) function OnLeave(player) local prefix = "Player_"..player.UserId local sucess, err = pcall(function() stat_save:SetAsync(prefix, --It should save value here stat_save[1]) print("saved in datastore: "..stat_save[1]) --prints nothing end) end game.Players.PlayerRemoving:Connect(OnLeave)