So here's what I wrote:
local serverStorage = game:GetService('ServerStorage') local dataStoreService = game:GetService('DataStoreService') local statistics = dataStoreService:GetDataStore('general','statistics') local getData = Instance.new('BindableFunction') getData.Name = 'GetData' getData.Parent = serverStorage local session = {} local DEFAULTDATA = { wins = 0; } function getKey(user) assert(user and typeof(user) == 'Instance' and user:IsA('Player'), 'first parameter for function "getKey" is of wrong type.') local key = 'user-'..user.UserId return key end --load: function get(user) assert(user and typeof(user) == 'Instance' and user:IsA('Player'), 'first parameter for function "get" is of wrong type.') local key = getKey(user) local success,value = pcall(function() return statistics:GetAsync(key) end) --stops execution here if success then if value ~= nil then return true,value end end return false end function getValue(user,request) assert(user and typeof(user) == 'Instance' and user:IsA('Player'), 'first parameter for function "getValue" is of wrong type.') assert(request and typeof(request) == 'string', 'second parameter for function "getValue" is of wrong type') local thisSession = session[user] if thisSession then if thisSession[request] ~= nil then return thisSession[request] end end return false end getData.OnInvoke = getValue --initialize: function init(user) assert(user and typeof(user) == 'Instance' and user:IsA('Player'), 'first parameter for function "init" is of wrong type.') local success,data = get(user) if success then if data ~= nil then session[user] = data else session[user] = DEFAULTDATA saveSession() end else warn('Insuccessfully initialized user '..user.Name..'\'s data.') end end --save: function saveSession(user) assert(user and typeof(user) == 'Instance' and user:IsA('Player'), 'first parameter for function "saveSession" is of wrong type.') statistics:UpdateAsync(function(oldValue) local newValue = session[user] or oldValue return newValue end) end getData.OnInvoke = getValue game.Players.PlayerAdded:connect(function(user) print('User '..user.Name..' has joined. Initializing data.') init(user) end)
Well, of course, it doesn't work in play mode. But it throws the "insuccessfully initialized" warning that I put there and when I debug it, it just stops the debugging on the pcall
of the function get
, and therefore doesn't move on to the if success then
condition(on line 33).
But I can't see anything wrong with this code, and I assume it should move on to the next
line after the pcall
. If you have anything in mind, please post an answer or at least a comment.
Thanks,
SwaggyDuckie.
Have you enabled datastores for the game? They are disabled by default.
Also, any errors thrown out in the developer console?