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

How do you save bool Values with dataStores?

This was my attempt.

local datastoreservice = game:GetService("DataStoreService") local mydatastore = datastoreservice:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Value = 0
Clicks.Parent = leaderstats

local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 2000000000
Coins.Parent = leaderstats

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, err = pcall(function()
	mydatastore:SetAsync(game.Players.LocalPlayer.PlayerGui:WaitForChild("Clicker").GrayMouse.HasItem.Value, game.Players.LocalPlayer.PlayerGui:WaitForChild("Clicker").GrayMouse.HasItem)
end)

if success then
	print("Success!")
else
	warn(err)
end

end)

3 comments

0

.GrayMouse.HasItem didn't you mean .GrayMouse.HasItem.Value?

0

Also, you didn't retrieve the value from the datastore.

0

You are using SetAsync(). But where is GetAsync()?

1 answer

This would have been my attempt if I was in your place. I would first have created a bool value inside the player. Let's call it 'HasItem'. Whenever you will have to change this value maybe from inside a local script, you will just change this bool value. Now our main challenge will be to save and load this bool value.

This is going to be my script inside serverscriptservice:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Value = 0
	Clicks.Parent = leaderstats

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 2000000000
	Coins.Parent = leaderstats
end)

This is your code so far.

Now I will add the bool value just like Clicks and Coins. But I won't add it to the leaderstats since we don't want to show the bool value on the leaderboard. Thus, I am going to make a sepearte folder for it. Like this:

	local HiddenFolder = Instance.new("Folder")
	HiddenFolder.Name = "HiddenFolder"
	HiddenFolder.Parent = Player
	
	local HasItem = Instance.new("BoolValue")
	HasItem.Name = "HasItem"
	HasItem.Value = false -- Change this to your needs
	HasItem.Parent = HiddenFolder

So our code so far is this:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Value = 0
	Clicks.Parent = leaderstats

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 2000000000
	Coins.Parent = leaderstats
	
	local HiddenFolder = Instance.new("Folder")
	HiddenFolder.Name = "HiddenFolder"
	HiddenFolder.Parent = Player
	
	local HasItem = Instance.new("BoolValue")
	HasItem.Name = "HasItem"
	HasItem.Value = false -- Change this to your needs
	HasItem.Parent = HiddenFolder
end)

Now our next challenge is to save and load the data. So here is how we will do it:

First, we will load the data. So inside the playeradded event:

local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(Player.UserId) -- You can do the same thing to save the other values too. E.g Clicks
	end)
	
	if success and data then
		HasItem.Value = data.HasItem
	else
		print("There was an error whilst loading the player's data!")
		warn(errormessage)
	end

And now, when the player leaves the game, we will load that saved data. So inside the same script, player removing event:

game.Players.PlayerRemoving:Connect(function(Player)
	local success, errormessage = pcall(function()
		local data = {
			HasItem = Player.HiddenFolder.HasItem.Value;
			-- Just add the other values here too.
		}
		
		DataStore:SetAsync(Player.UserId, data)
		
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error whilst saving the player's data!")
		warn(errormessage)
	end
	
end)

So this is our whole script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Value = 0
	Clicks.Parent = leaderstats

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 2000000000
	Coins.Parent = leaderstats
	
	local HiddenFolder = Instance.new("Folder")
	HiddenFolder.Name = "HiddenFolder"
	HiddenFolder.Parent = Player
	
	local HasItem = Instance.new("BoolValue")
	HasItem.Name = "HasItem"
	HasItem.Value = false -- Change this to your needs
	HasItem.Parent = HiddenFolder
	
	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(Player.UserId)
	end)
	
	if success and data then
		HasItem.Value = data.HasItem
	else
		print("There was an error whilst loading the player's data!")
		warn(errormessage)
	end
	
end)


game.Players.PlayerRemoving:Connect(function(Player)
	local success, errormessage = pcall(function()
		local data = {
			HasItem = Player.HiddenFolder.HasItem.Value;
		}
		
		DataStore:SetAsync(Player.UserId, data)
		
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error whilst saving the player's data!")
		warn(errormessage)
	end
	
end)

Didn't try this script though, but hopefully it will/should work.

1 comment

0

I think you made it a bit over complicated and harder to understand, he needs to just save a simple value.

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