I’ve created a BoolValue and placed it in ReplicatedStorage. Whenever I click a gui button it’ll set the BoolValue’s value to false. I’ve created this DataStore that seems completely correct and it’s just setting the BoolValue’s value back to true whenever I rejoin the game after clicking the button.
Script:
local DataStoreService = game:GetService("DataStoreService")
local TestingDataStore = DataStoreService:GetDataStore("TestingDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local PlayerID = Player.UserId
local Success, Error = pcall(function()
return TestingDataStore:GetAsync(PlayerID)
end)
local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue").Value
if Success then
print(SavingValue)
else
warn(Error)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local PlayerID = Player.UserId
local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue").Value
local Success, Error = pcall(function()
TestingDataStore:SetAsync(PlayerID, SavingValue)
end)
if Success then
print("Data saved!")
else
warn(Error)
end
end)
you must insert something like this in the script:
local data = nil
local PlayerID = Player.UserId
local Success, Error = pcall(function()
data = TestingDataStore:GetAsync(PlayerID)
end)
local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue")
if Sucess then
SavingValue.Value = data
end
also you can’t change a bool var cuz thats the same thing as
local transparency = part.Transparency
transparency = 1
local transparency = 0
transparency = 1
0 = 1
they’re all the same thing, so, next time, do not make a var ith the bool in it
-- CLIENT
local RemoteEvent = game:GetService('ReplicatedStorage'):WaitForChild('RemoteEvent')
RemoteEvent:FireServer(BoolValue)
-- SERVER
local RemoteEvent = game:GetService('ReplicatedStorage').RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(Player, BoolValue)
-- Save 'BoolValue' to your datastore.
end)
Just to verify. I tried the following and it didn’t work. I’m getting the warn error.
game.Players.PlayerRemoving:Connect(function(Player)
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(Value)
local Success, Error = pcall(function()
TestingDataStore:SetAsync(Player.UserId, Value)
end)
if Success then
print("Success")
print(Value)
else
warn("No")
end
end)
end)
local DataStoreService = game:GetService("DataStoreService")
local TestingDataStore = DataStoreService:GetDataStore("TestingDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local PlayerID = Player.UserId
local Data
local Success, Error = pcall(function()
Data = TestingDataStore:GetAsync(PlayerID)
end)
local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue").Value
if Success then
SavingValue = Data
wait(1)
print(SavingValue)
else
warn(Error)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(Value)
local Success, Error = pcall(function()
TestingDataStore:SetAsync(Player.UserId, Value)
end)
if Success then
print("Success")
print(Value)
else
warn("No")
end
end)
end)