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

how can I access a players data when they aren't in the game?

I'm making an admin system that you basically type a players name in then it will load a list of their data into a gui and from there you can edit it.

Now I save the players data in a table, and that seems to cause a problem when I try loading stuff in.

Here's the server script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("t-t-t2")

game.ReplicatedStorage.AdminFunction.OnServerEvent:Connect(function(plr, target)
	
	print("it got here?")
	
	local id = game.Players:GetUserIdFromNameAsync(target) --The id is our key
	
	local key = "stats-"..id
	
	print(key)
	
	local data = DataStore:GetAsync(key)
	
	print(data)
	
end)

and here's the local script:

script.Parent.MouseButton1Click:Connect(function()
	
	local isAdmin = game.ReplicatedStorage.IsAdmin:InvokeServer(game.Players.LocalPlayer.Name)
	local targetPlayer = script.Parent.Parent.PlayerName
	
	
	if isAdmin == true then
		
		print("ok")
		
		game.ReplicatedStorage.AdminFunction:FireServer(targetPlayer.Text) --Firing the name to the server dumbass
		
	end
	
end)

2 answers

If you have the player ID change the local id in Line 07 to the player's id so it will load that players id so like.

local DataStore = game:GetService("DataStoreService"):GetDataStore("t-t-t2")

game.ReplicatedStorage.AdminFunction.OnServerEvent:Connect(function(plr, target)

    print("it got here?")

    local id = --THEPLAYERSIDHERE--

    local key = "stats-"..id

    print(key)

    local data = DataStore:GetAsync(key)

    print(data)

end)

This should work but if not i don't know.

Alright, so you are trying to make a data editor. To get this, you are doing a call to the server and the server gets the requested data.

The problem?

You aren't doing anything with the data when you get it. You must return the data to the client (the "admin" in your case) for they to edit it. To do so, you must replace the "AdminFunction" RemoteEvent with a RemoteFunction.

I also noticed a big security issue. It is that the server is directly getting the data without checking the player that fired it so an exploiter can fire this easily with their victim's name. And then edit the data to their pleasure.

The new code would look like this on the server.

-- Server

local DataStore = game:GetService("DataStoreService"):GetDataStore("t-t-t2")

game:GetService("ReplicatedStorage").AdminFunction.OnServerInvoke = function(plr, target)

    print("it got here?")

-- Please check that the user is an admin because an exploiter could make his own datastore editor on your game.

    local id = game.Players:GetUserIdFromNameAsync(target) --The id is our key

    local key = "stats-"..id

    print(key)

	local Success,Data = pcall(function() -- GetAsync can error due to the datastore being down, poor connection, etc.
		return DataStore:GetAsync(key)
	end)
	
	if Success then -- We succeded in getting the data! Let's return it.
		return Data
	else
		-- The data store failed. Show an error message to the admin?
	end
end

And on the client:

-- Client

script.Parent.MouseButton1Click:Connect(function()

    local isAdmin = game:GetService("ReplicatedStorage").IsAdmin:InvokeServer(game.Players.LocalPlayer.Name)
    local targetPlayer = script.Parent.Parent.PlayerName


    if isAdmin == true then

        print("ok")

        local Data = game:GetService("ReplicatedStorage").AdminFunction:InvokeServer(targetPlayer.Text)

	-- Do whatever you want with the data. But after your admins are done, invoke the server again to save it to the data store.
    end

end)

2 comments

0

Hey, sorry for late reply, but the Data is returned as nil. Thanks for your help though. Could you possibly get back to me?

0

Is if is nil, then it maybe because the player does not have any data saved yet.

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