Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

0

I have edited my answer, check it out now

1 answer

✓ Accepted answer

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.

3 comments

0

I did it But it Says that SetAsync is not a valid member of DataStoreService "DataStoreService"

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.