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