Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How can you save a bool value or something with datastore?

Asked by 4 years ago
Edited 4 years ago

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.

01-- This is a local script on the TextButton when click it changes.
02local Choose = game.ReplicatedStorage.Choose
03 
04script.Parent.Text = "Not Pressed Yet!"
05script.Parent.MouseButton1Click:Connect(function()
06    if Choose.Value == false then
07        script.Parent.Text = "True"
08        Choose.Value = true
09elseif Choose.Value == true then
10        script.Parent.Text = "False"
11        Choose.Value = false
12end
13end)
01-- This Script is on ServerScriptService
02-- The Bool Value Which is called Choose is on Replicated Storage
03local DataStore = game:GetService("DataStoreService")
04local GetData = DataStore:GetDataStore("ValueDataStore")
05local Choose = game.ReplicatedStorage.Choose
06 
07game.Players.PlayerAdded:Connect(function(player)
08local data 
09    local Success,error pcall(function()
10            data = DataStore:GetAsync(player.UserId)
11        if Success then
12            Choose.Value = data
13        else
14            warn(error)
15        end
View all 31 lines...
0
I have edited my answer, check it out now User#32819 0 — 4y

1 answer

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago

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:

01-- create a RemoteEvent in ReplicatedStorage, and set the name to "changeChoose"
02 
03-- this will be the local script
04 
05local chooseEvent = game.ReplicatedStorage:WaitForChild("changeChoose")
06 
07script.Parent.Text = "Not Pressed Yet!"
08 
09script.Parent.MouseButton1Click:Connect(function()
10 
11    if Choose.Value == false then
12 
13        script.Parent.Text = "True"
14        chooseEvent:FireServer(true) -- we can use the arguments to our advantage
15 
View all 69 lines...

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.

0
I did it But it Says that SetAsync is not a valid member of DataStoreService "DataStoreService" mastercheeseit 77 — 4y
0
Thank you for your time i really appericate it but i didn't get what you meant by SetAsync was not a valid member of DataStoreService because you have to do it on the actual datastore, not the service. mastercheeseit 77 — 4y
0
It means you have to SetAsync on a datastore, because the service is to GET the datastore. In this case, DataStore is the SERVICE, and GetData is the ACTUAL DATASTORE. User#32819 0 — 4y
Ad

Answer this question