How to retrieve specific player data from datastore

I’ve been going through posts somewhat like this for about an hour and I’m honestly getting nowhere so I’m making a quick post. I’m sure this is pretty simple to do ? but I’m not great with scripting so either way help would be appreciated. Essentially what I need for a certain system, is to look up a player’s data from the datastore even if the specified player isn’t in-game; really all that’s needed is to get the value from their data as a variable. How would I do this?

Use player id as part of the datastore key

local playerCoins = mydatastore:GetAsync(tostring(player.UserId) .. "Coins")

You can get the player’s user id in many ways, such as GetUserIdFromNameAsync, or if they are in the game simply player.UserId If there is a specific way you’re looking to get a player’s ID please tell us.

So I added that in, but it started giving an error saying that datastores can’t be accessed through the client - so I changed the remote to fire through the server and collect the data there, but now it is giving an error saying that :FireServer can only called by the client. Do you know why this could be?

Are you using a local script to fire the remote event?

No, it’s just a regular script

That’s the problem only a client can fire a remote event, you can use bindable event if you want to fire an event server to server.

If a player want to access someone’s data then you have to use remote function to return the data, or you could d use the remote event and just print the data or just change the ui there.

I also recommend that you use tick() or os.time() to check if the remote is being spammed or not. I made a script in the past to check if the event is being spammed or not:

local lastCall = {}

event.OnServerEvent:Connect(function(player, data)
	if not lastCall[player.Name] then
		lastCall[player.Name] = {0, 0, 0}
	else
		if (math.max(tick() - lastCall[player.Name][1], 0.001)) < 0.3 then
			if (tick() - lastCall[player.Name][3]) > 5 then
				lastCall[player.Name][2] += 1
				lastCall[player.Name][3] = tick()

				warn(player.Name.." is consecutively fireing the remote event.\nWarnning:", lastCall[player.Name][2])

				if lastCall[player.Name][2] > 3 then
					player:Kick(player.Name, "is consecutively fireing the remote event")
				end
			end
		else
			lastCall[player.Name][3] = tick()
		end
	end

	lastCall[player.Name][1] = tick()
end)
3 Likes

Oh okay! I’m actually going out now but I’ll let you know how it goes once I’m back :smile:

1 Like

So I just tried this out and it seems to be working fine with server - server - client, but the only issue now is it keeps returning the player data as nil when they have saved data. I have put in the line that @gertkeno mentioned earlier, but I’m not too sure if there’s something I did wrong sending the data to the client? Help from anyone would be appreciated, I am in no way a great scripter :running_woman:

remote getting data:

game.ServerStorage.InfoCommand.Event:Connect(function(Player, PlayerName)
	local PlayerID = game:GetService("Players"):GetUserIdFromNameAsync(PlayerName)
	local Days = DayData:GetAsync(tostring(PlayerID) .. "Days")
	if Days == nil then
		print("error while getting player data")
	else 
		Remote:FireClient(Player, PlayerName, Days)
	end
end)
1 Like

Never mind, I just figured it out by changing a few things! Thank you to those who helped :heart:

1 Like