Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Where do I store client data?

Asked by 2 days 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?

0
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 21 — 2d

1 answer

Log in to vote
0
Answered by 2 days ago

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

-- CLIENT SCRIPT:

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

    remoteEvent:FireServer(number)
end)

-- SERVERSCRIPT:

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)
    end

    -- 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)
end)

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.

Ad

Answer this question