If data is saved then it will print error. My script print Error. It should have saved. what did I do wrong
local DataStoreService = game:GetService("DataStoreService") local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local points = Instance.new("IntValue", leaderstats) points.Name = "Points" local PlayerID = "player"..player.UserId local PlayerData local success, errormessage = pcall(function() PlayerData = PlayerDataStore:GetSync(PlayerID) end) if success then points.Value = PlayerData end end) game.Players.PlayerRemoving:Connect(function(player) local PlayerID = "player"..player.UserId local data = player.leaderstats.Points.Value local success, errormessage = pcall(function() PlayerDataStore:SetSync(PlayerID, data) end) if success then print ("data saved") else print ("error") end end)
Howdy!
It's :SetAsync()
, not :SetSync()
. Look at line 14 and 28. You can read more about this here.
If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.
This is what it says
--[[ Calls a function and throws an error if it attempts to yield. Pass any number of arguments to the function after the callback. This function supports multiple return; all results returned from the given function will be returned. ]] local function resultHandler(co, ok, ...) if not ok then local message = (...) error(debug.traceback(co, message), 2) end if coroutine.status(co) ~= "dead" then error(debug.traceback(co, "Attempted to yield inside changed event!"), 2) end return ... end local function NoYield(callback, ...) local co = coroutine.create(callback) return resultHandler(co, coroutine.resume(co, ...)) end return NoYield