Transferring dataPersistance to datastoreService

So im currently making a modded nds game but im having trouble converting this dataPersistance code to datastoreservice. I already have a datastore key called “Survived” so thats what I want to convert it to.

I have no idea on how to do this stuff so it would be awesome if somebody could help.

function getSurvives(player)
	if player then
		local userId = player.UserId
		local cachedWins = playersWins['id'..userId] or 0
		if playersWins['id'..userId] then
			return cachedWins
		end
		local playerPointsWins = 0
		local playerPointsSuccess, playerPointsReturn = nil, nil


		local dataPersistanceWins = 0
		local dataPersistanceSuccess, dataPersistanceReturn = nil, nil
		spawn(function()
			dataPersistanceSuccess, dataPersistanceReturn = pcall(function()
				if player:WaitForDataReady() then
					local dataPersistanceWins = player:LoadNumber("SurvivedTotal") or 0
				end
			end)
			if dataPersistanceSuccess and playerPointsReturn then
				dataPersistanceWins = dataPersistanceReturn
			end
		end)

		local dataStoreWins = 0
		local dataStoreSuccess, dataStoreReturn = nil, nil
		spawn(function()
			dataStoreSuccess, dataStoreReturn = pcall(function()
				return survivesDataStore:GetAsync('id'..userId)
			end)
			if dataStoreSuccess and playerPointsReturn then
				dataStoreWins = dataStoreReturn
			end
		end)

		local startWaitTick = tick()
		while true do
			if (tick() - startWaitTick) > 10 then
				break
			end
			if playerPointsSuccess~=nil then
				break
			end
			wait()
		end

		local finalWins = math.ceil(math.max(math.max(playerPointsWins, playerPointsWins), playerPointsWins))
		playersWins['id'..userId] = finalWins
		return finalWins
	end
	return 0
end
1 Like