So I just watched a video on Datastore and learned that you can save data even when you leave the server so here I tried to test it here look at my code but for some reason it won't save
I want to change it's value with an textbutton when it is clicked we turn true to bool value and when we click again it turns to an false value and changes the Bool Value which is Named choose to a False value and if i leave it False then it should say false and save it but it won't here is the scripts.
Btw The Enable Studio To API Services is turned on.
-- This is a local script on the TextButton when click it changes. local Choose = game.ReplicatedStorage.Choose script.Parent.Text = "Not Pressed Yet!" script.Parent.MouseButton1Click:Connect(function() if Choose.Value == false then script.Parent.Text = "True" Choose.Value = true elseif Choose.Value == true then script.Parent.Text = "False" Choose.Value = false end end)
-- This Script is on ServerScriptService -- The Bool Value Which is called Choose is on Replicated Storage local DataStore = game:GetService("DataStoreService") local GetData = DataStore:GetDataStore("ValueDataStore") local Choose = game.ReplicatedStorage.Choose game.Players.PlayerAdded:Connect(function(player) local data local Success,error pcall(function() data = DataStore:GetAsync(player.UserId) if Success then Choose.Value = data else warn(error) end end) end) game.Players.PlayerRemoving:Connect(function(player) DataStore:SetAsync(player.UserId,Choose.Value) local Success,error pcall(function() if Success then print("Okay") else print("No") warn(error) end end) end)
It is not being saved because you are changing the value on the client, and the server doesn't see the change. What you should do is use a RemoteEvent to change it on the server. Here's how you set it up:
-- create a RemoteEvent in ReplicatedStorage, and set the name to "changeChoose" -- this will be the local script local chooseEvent = game.ReplicatedStorage:WaitForChild("changeChoose") script.Parent.Text = "Not Pressed Yet!" script.Parent.MouseButton1Click:Connect(function() if Choose.Value == false then script.Parent.Text = "True" chooseEvent:FireServer(true) -- we can use the arguments to our advantage elseif Choose.Value == true then script.Parent.Text = "False" chooseEvent:FireServer(false) end end) -- this will be the server script: local DataStore = game:GetService("DataStoreService") local GetData = DataStore:GetDataStore("ValueDataStore") local Choose = game.ReplicatedStorage.Choose local chooseEvent = game.ReplicatedStorage.changeChoose game.Players.PlayerAdded:Connect(function(player) local data local Success,error = pcall(function() data = GetData:GetAsync(player.UserId) -- you should only make data something AFTER the pcall end) if Success then Choose.Value = data else warn(error) end end) game.Players.PlayerRemoving:Connect(function(player) GetData:SetAsync(player.UserId,Choose.Value) -- removed this pcall because it is literally unnessary end) -- now for our remote event chooseEvent.OnServerEvent:Connect(function(player,status) -- first argument is always the player, then the arguments we passed on the local script Choose.Value = status -- since status is false or true, we can set the value to it end)
Remember exploiters can fire the remote and change it to false or true whenever they want, so depending on what you're doing you will be able to secure the remote. Edit: it was saying SetAsync was not a valid member of DataStoreService because you have to do it on the actual datastore, not the service. I've fixed the script.