Datastore2 help: Get player data even if that player is no longer in the game

I am new to Datastore2, but I am using it for my soon to be released game.

I have a system where players can pay 25 R$ to play a song, and it is queued. This whole system works fine, however, I’m trying to make it so that if a server shuts down as an example, then when the player rejoins it re-queues their song. This is so that if I ever need to shut down all servers for something, I don’t have a riot of refund requests.

	if #DJFolder:GetChildren() > 0 then --If there are queued songs
		CustomSong = true
		local Chosen = nil
		local Lowest = math.huge
		local Player
		for _,v in pairs(DJFolder:GetChildren()) do
			if v.PosValue.Value < Lowest then --Gets who is at the top of the queue
				Chosen = v
				Lowest = v.PosValue.Value
				Player = v.PlayerValue.Value
			end
		end
		pcall(function()
			local queuedSongDataStore = DataStore2("QueuedSong",Player)
			queuedSongDataStore:Set(0)
			Player.DJ_ID.Value = 0
		end)
		PlaySound(Chosen)
		Chosen:Destroy()
		ShopEvent:FireAllClients("DJ_QueueUpdated")
	else
		local RndmTable = {}
		if #CurrentSoundsFolder:GetChildren() <= 0 then
			for i,Sound in pairs(SoundsTings:GetChildren()) do
				local Clone = Sound:Clone()
				Clone.Parent = CurrentSoundsFolder
			end
		end
		
		for _,v in pairs(CurrentSoundsFolder:GetChildren()) do
			table.insert(RndmTable,v)
		end
		RndmTable = shuffle(RndmTable)
		
		local ChosenSound = RndmTable[math.random(1,#RndmTable)]
		PlaySound(ChosenSound)
		ChosenSound:Destroy()
	end

Here is the section where I am planning to make it so that it tells the datastore that the player’s song has been played so that when they join a new server it doesn’t queue their song thinking an error occurred in the last one

My ultimate problem is that the Datastore module wants the player object, not the players UserId. So if a player is no longer in the game wouldn’t this cause an error? If anyone knows what I should do that would be appreciated thanks.

1 Like

I can confirm after trying that in game, that if a player leaves after queuing their song the

		pcall(function()
			local queuedSongDataStore = DataStore2("QueuedSong",Player)
			queuedSongDataStore:Set(0)
			Player.DJ_ID.Value = 0
		end)

doesn’t work, so when they rejoin again it simply just plays the song again, letting them abusive this and play the song inf amount of times as long as they leave before the song plays.

I’d suggest using DataStoreService for this instead of DataStore2. You don’t really need player’s data, only the song ID.

1 Like

Yeah, just used normal datastoreservice with getasync and setasync.