Help with get DataStore Information using TextBox

Im making a UI that shows a TextBox and a TextButton and u can write in TextButton a UserId and when click in TextButton the TextBox text change to the DataStore information of the UserId.

But i can’t make it

Normal Script Inside a TextButton:

local DataStore = game:GetService("DataStoreService")
local DS1 = DataStore:GetDataStore("DS1")
local DS2 = DataStore:GetDataStore("DS2")
local text = script.Parent.Parent.TextBox.Text

script.Parent.MouseButton1Click:Connect(function()
    local d = DS1:GetAsync(text)
-- function
end)

But the script doesn’t make anything and idk why (don’t print any errors)
And i want to know if its possible to make what im thinking.
If u know a script more easy and better than this, that makes the same i want please show the script here.

4 Likes

You can just set the data value to TextBox.Text

Also, run :SetAsync() and :GetAsync() in a pcall().
Something like this (you might need to change it to suit what you had in mind):

script.Parent.MouseButton1Click:Connect(function()
    local success, errormessage = pcall(function()
        local d = DS1:SetAsync(UserId, TextBox.Text) -- save data relative to the player's U-ID.
    end

    if success then
        print("Data saved!")
    else
        warn("Error saving data!")
        warn(errormessage)
    end
end)

also maybe consider debouncing this so that you dont flood the request queue.

2 Likes

but isnt possible make a script to see other player DataStore in a TextBox? (Off-line Player)
Im trying to make a script to see the datastore of others players

Other player’s data store?
Like I said, you might need to tweak that a bit.

Use two text boxes, one for the U-ID of the player you want to view, and one to set it.
Then replace it with TextLabel.Text = DS1:GetAsync(UserId) or something like that.

remember the pcall, though

script.Parent.MouseButton1Click:Connect(function()
    local data = nil
    local success, errormessage = pcall(function()
        data = DS1:GetAsync(TextBox.Text) -- save data relative to the player's U-ID.
    end

    if success then
        TextLabel.Text = data --define TextLabel and TextBox where needed
        print("Data saved!")
    else
        TextLabel.Text = "Incorrect input."
        warn("Error saving data!")
        warn(errormessage)
    end
end)
2 Likes

It is completely possible. The problem here is that the text variable is the original text of the textbox, so when the textbook’s text changes, the text variable won’t change. Try this:

local DataStore = game:GetService("DataStoreService")
local DS1 = DataStore:GetDataStore("DS1")
local DS2 = DataStore:GetDataStore("DS2")


script.Parent.MouseButton1Click:Connect(function()
     local text = script.Parent.Parent.TextBox.Text
    local d = DS1:GetAsync(text)
    print(d)
-- function
end)
2 Likes

its saying [101: Key name can’t be empty.]
i cant find this error in google

1 Like

Are you using SetAsync or GetAsync?

1 Like

Im using the command GetAsync()

1 Like

It needs a field for UserId,
hence why I reccomended Pcall.

If the player doesn’t type a UserId for it to search for it has no key to use and the script errors, stopping it from running.
Pcall allows you to inform them of that while keeping it from breaking.

It needs to be something like :GetAsync(TextBox.Text) --in the text box type the user id

1 Like

wait now i remember, i need the API actived, right? XD

1 Like

Yes, there’s that too :joy:

Make sure it’s on then try looking into how pcall() works and how it’s useful.

1 Like

wait, your scripts its giving a error in the “if” but its correct to me…
forget it

i think u forgot the “)” in end

1 Like

did you define TextLabel?
(tell it what it actually is)

1 Like

define the TextLabel? if u talking put the “local = things things … TextLabel” i put it in first line inside the MouseButton1Click

1 Like

TextBox too?
it’s getting the UserId from the TextBox. If the text box is empty it shouldnt do anything.

1 Like

yea, the two, now ill try use the SetAsync to see if shows it

1 Like

what? wait limit of likes i can give/day? ;-;

Its saying “Incorrect Input.”
So… its possible the TextBox gives the number in string?

1 Like

That’s what it should say,
you’ll notice where it does that in the ‘if’ statement.

That just means it was an empty field, but nothing went wrong while getting data.

You could now make another GUI with another text box to set the data for a UserId

1 Like

in the other gui its saying
attempt to index nil with “UserId”
I used:

local DataStore = game:GetService("DataStoreService")
local DS1 = DataStore:GetDataStore("DS1")
local player = game.Players.LocalPlayer


script.Parent.MouseButton1Click:Connect(function()
    DS1:SetAsync(player.UserId, "text")
end)