Log in to vote

Where do I store client data?

Asked by 7 months ago

Say that a player chooses a number between 1 and 5 in a GUI and the player chooses number 4, where/how can I place that piece of data so I can refer to it via server scripts?

Are you trying to send data form client to server? If so, try to make a value inside StarterGui and then use RemoteEvents to send it to server Feelings_La 384 — 7mo

1 answer

Log in to vote
Answered by 7 months ago

If you're trying to store data, use Datastores. do it like this:


local button1, button2, button3, button4, button5 = (where to locate the button with 1), (button with 2), (3), (4), (5)

local player = script:FindFirstAncestorOfClass("Player")

local remoteEvent = (directory of remote event you use)

button1.MouseButton1Down:Connect(function() -- in the other buttons, replace the number in the button name with the button number. example, button2.MouseButton1Down:Connect(function())
    local number = 1 -- number of the button



local client_datastore = game:GetService("DataStoreService"):GetDataStore("ClientData") -- you can also use ordered data stores by game:GetService("DataStoreService"):GetOrderedDataStore("ClientData"). datastores can be any name you want.

local remoteEvent = (directory of the same remote event in client script)

remoteEvent.OnServerEvent:Connect(function(player, number)
    local client_data = client_datastore:GetAsync(player.UserId, number)

    if client_data == nil then
        client_datastore:SetAsync(player.UserId, number)
        client_data = client_datastore:GetAsync(player.UserId, number)

    -- datastores can be accessed by other server scripts using :GetDataStore(datastore_name). to access the datastore, the datastore name should be exactly as the one mentioned above. you can check/get the datastore value by :GetAsync(player.UserId)

P.S. datastores can't be used in client scripts. Use remote events to trigger stuff in the client if you want something with the client script.


Answer this question