Hello,
I am trying to data store a text button’s visible property. Basically, when a player leaves it checks the textbutton’s visibility and upon joining their data loads. How can I accomplish this? I can’t find any documentation on this, and I started the script but I get stumped on how to actually save and load the property.
Heres the script:
local DSS = game:GetService("DataStoreService")
local Dstore = DSS:GetDataStore("TB")
game.Players.PlayerAdded:Connect(function(Player)
local Success, ErrorMessage = pcall(function()
Dstore = DataStore:GetAsync(Player.UserId)
-- where I get stuck
end)
end)
The way I’d go about doing this is creating a BoolValue inside the gui object, and whenever that object is visible, set the BoolValue to true, and then if it’s not visible, set it to false.
Upon a player being removed, I would save the value of the BoolValue, and then once the player joins the game, load their data and check if that BoolValue is true or false, and set the visibility to the appropriate value.
I tried this, but I am unsure how to check if the player has data. I tried this new script:
local DSS = game:GetService("DataStoreService")
local Dstore = DSS:GetDataStore("TB")
local value = game:GetService("StarterGui"):FindFirstChild("BaseUI"):FindFirstChild("OptionsTab"):FindFirstChild("pan"):FindFirstChild("furn")
game.Players.PlayerAdded:Connect(function(Player)
local Success, ErrorMessage = pcall(function()
Dstore = DataStore:GetAsync(Player.UserId, value.Value)
end)
if Success then
-- get stumped here
end)
I get stumped here. I don’t know how to load the bool value or anything.
You’ll wanna save the player’s data when they leave, so I would use a PlayerRemoving event and then get the player’s BoolValue for their gui object. You can’t use StarterGui, because everything in StarterGui is cloned into the PlayerGui, so changes will only be seen on the PlayerGui, therefore they should be made there. A way of doing this would be: local value = player.PlayerGui:FindFirstChild("BaseUI"):FindFirstChild("OptionsTab"):FindFirstChild("pan"):FindFirstChild("furn"), after this, we’ll use a pcall just like you did when getting the player’s data,
local Succes, errormessage = pcall(function()
Dstore:SetAsync(player.UserId, value.Value)
end)
Sorry for the bad formatting, on my phone.
In your :GetAsync(), make sure to get the BoolValue as well.
I also tried a newer script, the datastore script being:
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("Datastar")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local bool = Instance.new("BoolValue")
bool.Name = "Bool"
bool.Parent = player
local data
local success, result = pcall(function()
data = dataStore:GetAsync("Player_"..player.UserId)
end)
if success then
if data then
print(result)
bool.Value = data
else
print(result)
bool.Value = false
end
else
warn(result)
end
end)
players.PlayerRemoving:Connect(function(player)
local success, result = pcall(function()
dataStore:SetAsync("Player_"..player.UserId, player.Bool.Value)
end)
if success then
print(result)
else
warn(result)
end
end)
game:BindToClose(function()
for _, player in ipairs(players:GetPlayers()) do
local success, result = pcall(function()
dataStore:SetAsync("Player_"..player.UserId, player.Bool.Value)
end)
if success then
print(result)
else
warn(result)
end
end
end)
local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Parent = game.ReplicatedStorage
RemoteEvent.OnServerEvent:Connect(function(Player)
print(Player.name.." is firing from client to server!")
Player:FindFirstChild("Bool").Value = true
end)
And this local script changing firing the event:
local RemoteEvent = game.ReplicatedStorage:WaitForChild("Funny")
local Button = script.Parent
Button.MouseButton1Click:Connect(function()
RemoteEvent:FireServer()
script.Parent.Parent.pan.Visible = true
end)
I also have this new local script changing the text button’s visibility based on the value:
game.Players.PlayerAdded:Connect(function(Player)
local value = Player:FindFirstChild("Bool")
if value.Value == true then
script.Parent.Visible = true
else
script.Parent.Visible = false
end
end)
The script above does not work. No errors, it saves and load but the text button visibility is not changing. Can anyone help?
Oh wait, is that a local script? If so .PlayerAdded won’t fire for the local player (the client of which the script is executing for).
local players = game:GetService("Players")
local player = players.LocalPlayer
local bool = player:WaitForChild("Bool")
local label = script.Parent
label.Visible = bool.Value
bool.Changed:Connect(function(newBool)
label.Visible = newBool
end)
You join, your data loads in, the game checks the data, the bool is true so the textbutton should be visible true but it should save whenever it changes.