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

getasync() not working to load play time data?

Basically what my code does is save an int value that's the seconds you've played in the game. The problem is that when you join and onplayeradded is ran, that timeplayed value wont load and it will start back from 0

dss=game:GetService('DataStoreService')

inventorystore=dss:GetDataStore('InventoryStore')
statsstore=dss:GetDataStore('StatsStore')

function storeconverter(store)
	if store=='InventoryStore'then
		store=inventorystore
	elseif store=='StatsStore'then
		store=statsstore
	end
	return store
end

function load(plr,store,storetype)
	newstore=storeconverter(store)
	local success,errormessage=pcall(function()
		return newstore:GetAsync(plr.UserId..storetype)
	end)
	if not success then

		warn(errormessage)
	else
		warn('success save')
	end
end

function save(plr,store,storetype,data)
	newstore=storeconverter(store)
	local success,errormessage=pcall(function()
		newstore:SetAsync(plr.UserId..storetype,data)
	end)
	if not success then

		warn(errormessage)
	else
		warn('success save')
	end
end

game.Players.PlayerAdded:Connect(function(plr)--init
	local plrid=plr.UserId
	
	thething=load(plr,'StatsStore','-timeplayed')
	local leaderstats=game.ReplicatedFirst.leaderstats:Clone()
	leaderstats.timeplayed.Value=thething
	leaderstats.Parent=plr
	
end)
game.Players.PlayerRemoving:Connect(function(plr)--savedata
		--save all data
		save(plr,'StatsStore','-timeplayed',plr.leaderstats.timeplayed.Value)
		

		print('complete save')
end)

The key plr.userid-timeplayed is saved in a store called statsstore. anyone know why the loading wont work?

1 answer

SOLVED apparently return doesnt work in pcalls i think?

working code for the load function

function load(plr,store,storetype)
	newstore=storeconverter(store)
	local success,errormessage=pcall(function()
		newstore=newstore:GetAsync(plr.UserId..storetype)
	end)
	if not success then
		--failsafe:SetAsync(plr.UserId,true)
		--plr:Kick('Error occured while loading data')
		warn(errormessage)
	else
		--failsafe:SetAsync(plr.UserId,false)
		warn('success load')
		return newstore
	end
end

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