How do I save the codes?

Local Script:

wait(0.3)

local codes = {"RELEASE"}
local event = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").codeEvent
local button = script.Parent.Parent.MainButtons.codesButton
local codeGui = script.Parent.Parent.codeGui

local tween = game:GetService("TweenService")

local verifyButton = codeGui.mainCodesGui.verifyButton

local function code()
	if codeGui.mainCodesGui.codeHere.Text == codes[1] then
		event:FireServer(100, codes[1])
	end
end

local function open()
	game:GetService("SoundService").click:Play()
	local Info = TweenInfo.new(0.15)
	local Tween = tween:Create(codeGui.mainCodesGui, Info, {Position = UDim2.new(0.214, 0, 0.251, 0)})
	Tween:Play()

	game:GetService("Lighting").Blur.Enabled = true
	tween:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 90}):Play()
end

local function close()
	game:GetService("SoundService").click:Play()
	local Info = TweenInfo.new(0.15)
	local Tween = tween:Create(codeGui.mainCodesGui, Info, {Position = UDim2.new(0.214, 0, 1, 0)})
	Tween:Play()

	game:GetService("Lighting").Blur.Enabled = false
	tween:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70}):Play()
end

verifyButton.MouseButton1Click:Connect(code)
verifyButton.TouchTap:Connect(code)

button.MouseButton1Click:Connect(open)
button.TouchTap:Connect(open)

codeGui.mainCodesGui.close.MouseButton1Click:Connect(close)
codeGui.mainCodesGui.close.TouchTap:Connect(close)

Server:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").codeEvent

event.OnServerEvent:Connect(function(player, reward, code)
	if player:FindFirstChild(code) == nil then
		local redeemed = Instance.new("BoolValue", player)
		redeemed.Name = code
		redeemed.Value = false
		
		if redeemed.Value == false then
			player.leaderstats.Wins.Value += reward
			redeemed.Value = true
		end
	end
end)

So here I have a code system like you write a code and get a reward. But I have a question how to make it so that when you leave the game system remembered that you have already written a code, and you can not write it again. In general, how to save that he applied the code?

2 Likes

I know I should use dataStore, but how exactly do I save a boolValue if I change its name?

1 Like

Check out this tutorial, It should clear your queries.

1 Like

is this what you mean ?

local Codes = {
["CodeHere"] = false,
["OtherCode"] = true
}
1 Like

Yeah, that’s what I mean. I need to check if the player has already used the code. Based on my script.

1 Like

you could save that whole table (dictionary) as data for the player, loads it, check if the code is in there, and if its true or false

2 Likes