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)

1 comment
I have edited my answer, check it out now