Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this DataStore script stop the execution completely before it reaches the end?[SOLVED]

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.

1 answer

Have you enabled datastores for the game? They are disabled by default.

Also, any errors thrown out in the developer console?

4 comments

0

All it throws is API Services rejected request with error: HTTP 0 (HTTP 403 (HTTP/1.1 403 Forbidden)). Also this is a local file place, which might explain. What I am really looking for is answer to if it's local place file is it still supposed to skip parts of my code after pcall?

0

It skips the rest because success equals to false. You can't use the datastore service without having an actual place running, so it throws an error which pcall detects and returns false with the other argument.

0

It shouldn't do that, but the rest is right, it's not an actual place running, so it skips the rest. If it is an actual place running, the pcall will work returning the success or not and continuing the code.

0

Your code will probably run just fine in a game, but there was always an issue when it came to testing data stores, though you can easily bypass that issue. Just create new data when pcall returns false and if the code is running in studio to simulate a real environment.

This discussion is preserved as it was. New answers, comments, and votes are closed.