How would I save bool value's for my game?

I am making a game which has shops for in-game currency and I need to save whether the player owns the items in the shop or not while using data store and bool values I have a local script which is located within the shop GUI with a script that allows the item to be purchased but now I just need the script to save the bool value here are the scripts:

LOCAL SCRIPT

local function purchaseFunc()
	if selectedTag.Value == "survivor" then
		if itemFrame:WaitForChild('Survivor'):WaitForChild('Owned').Value == false then
			if player:WaitForChild('leaderstats'):WaitForChild('Money').Value > 50 then
				game:GetService("ReplicatedStorage"):WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('ShopEvents'):WaitForChild('tagShopEvents'):WaitForChild('TagShopSurvivorPurchased'):FireServer(player)
				purchase.Text = 'Check Inventory To Equip "Survivor" tag!'
			elseif itemFrame:WaitForChild('Survivor'):WaitForChild('Owned').Value == true then
				purchase.Text = 'You Already Own "Survivor" tag!'
			end
		end
	end
end


local function survivor()
	descriptionPrice.Text = "$50"
	descriptionTitle.Text = "Survivor Tag"
	selectedTag.Value = "survivor"
end

local function fighter()
	descriptionPrice.Text = "$50"
	descriptionTitle.Text = "Fighter Tag"
	selectedTag.Value = "fighter"
end

local function killer()
	descriptionPrice.Text = "$175"
	descriptionTitle.Text = "Killer Tag"
	selectedTag.Value = "killer"
end

purchase.MouseButton1Click:Connect(purchaseFunc)
itemFrame:WaitForChild('Killer').MouseButton1Click:Connect(killer)
itemFrame:WaitForChild('Fighter').MouseButton1Click:Connect(fighter)
itemFrame:WaitForChild('Survivor').MouseButton1Click:Connect(survivor)

Server Script

local event = game:GetService('ReplicatedStorage'):WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('ShopEvents'):WaitForChild('tagShopEvents'):WaitForChild('TagShopEvent')
local survivor_bool = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('TagShopValues'):WaitForChild('SurvivorBool')
local fighter_bool = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('TagShopValues'):WaitForChild('SurvivorBool')
local killer_bool = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('TagShopValues'):WaitForChild('SurvivorBool')
local dataStoreService = game:GetService('DataStoreService')

local function purchase(player, value)
	if value == "survivor" then
		player:WaitForChild("leaderstats"):WaitForChild('Money').Value -= 50
		survivor_bool.Value = true
	end
	if value == "fighter" then
		player:WaitForChild("leaderstats"):WaitForChild('Money').Value -= 50
		fighter_bool.Value = true
	end
	if value == "killer" then
		player:WaitForChild("leaderstats"):WaitForChild('Money').Value -= 50
		killer_bool.Value = true
	end
end

event.OnServerEvent:Connect(purchase)

I need to save the bool values from the server script once the event is called for them and save the value to the data store, Thanks,

Use a dict with the item name as the key and the bool as the value. Heres an example:

-- create dictionary --
local isOwned = {gun = false, sword = true}

-- you can add to table like this --
isOwned[keyName] = value

On the datastore docs you can find how to store a table.

EDIT: you can create a remote event to check from the client to the server if the item is owned, check out the remote docs

Hello, Here is a script i used in my game, i hope it can help you.


local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Configuration")

game.Players.PlayerAdded:Connect(function(player)
	local folder = player:WaitForChild("Config")
	local boolValues = folder:GetChildren()

	for _, boolValue in ipairs(boolValues) do
		boolValue.Value = ds:GetAsync(player.UserId .. boolValue.Name) or false
		ds:SetAsync(player.UserId .. boolValue.Name, boolValue.Value)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local folder = player:FindFirstChild("Config")
	if folder then
		local boolValues = folder:GetChildren()

		for _, boolValue in ipairs(boolValues) do
			ds:SetAsync(player.UserId .. boolValue.Name, boolValue.Value)
		end
	end
end)
2 Likes