Is there a way to make this tell if a player used the code if they rejoined the game?

I was making a code GUI, but I got stuck on this part, is there a way to make it so when players rejoin the game they can’t enter the code again, if they already entered it, or a way to tell if a player entered the code even if they rejoin the game? This is what the localscript is.

local TextBox = script.Parent
local TextButton = script.Parent.Parent.TextButton
local Code = "FreeMoney"
local CodeUsed = false
TextButton.MouseButton1Click:Connect(function() --If the button is clicked.
	if TextBox.Text == Code and CodeUsed == false then
		game.Players.LocalPlayer.leaderstats.Gold.Value = game.Players.LocalPlayer.leaderstats.Gold.Value + 500
		CodeUsed = true
	else do
			print("Code was already used!")
		end
	end
end)

You could log what players have used the code in a table on the server.

1 Like

Use datastores and log it as a BoolValue

1 Like

Okay I have something!


Instances


In ServerScriptService create 2 Scripts, named SetData and AccessPlayerDataStore
Set SetData's Disabled parameter to true


In your UI create a LocalScript named EventDetect.


In ReplicatedStorage create 2 RemoteEvent, name these CodeEntered and ReturnData


Code


In SetData write:

local dss = game:GetService("DataStoreService")
local rep = game:GetService("ReplicatedStorage")
local ds = dss:GetDataStore("ds")
local event = rep.CodeEntered

local function start()
	local success, err = pcall(function()
		ds:SetAsync("HasBeen", true)
		ds:SetAsync("CodeEntered", false)
	end)
	local success, HasBeen = pcall(function()
		return ds:GetAsync("HasBeen")
	end)
	if success then
		script.Disabled = true
	end
end
start()

In AccessPlayerDataStore write:

local dss = game:GetService("DataStoreService")
local rep = game:GetService("ReplicatedStorage")
local ds = dss:GetDataStore("ds")
local event = rep.CodeEntered
local event2 = rep.ReturnData

local function start()
	repeat
		wait(2)
		print("Waiting... ")
	until script.Parent.SetData.Disabled == true
end
local function rest()
	event.OnServerEvent:connect(function(plr)
		do
			local success, HasBeen = pcall(function()
				print("Got datastore")
				return ds:GetAsync("HasBeen")
			end)
			if HasBeen == true then
				script.Parent.SetData.Disabled = true
				local success, codeentered = pcall(function()
					return ds:GetAsync("CodeEntered")
				end)
				if codeentered == true then
					event2:FireClient(plr)
				elseif codeentered == false then
					ds:UpdateAsync("CodeEntered", function()
						local val = true
						return val
					end)
				end
			elseif HasBeen == false then
				script.Parent.SetData.Disabled = false
			end
		end
	end)
end

start()
rest()

And finally in EventDetect write:

local textBox = script.Parent.Main.TextBox
local button = script.Parent.Main.Continue
local rep = game:GetService("ReplicatedStorage")
local event = rep.CodeEntered
local code = "Code"

local function readDataStore()
	if textBox.Text == code then
		textBox.TextColor3 = Color3.new(0,255,0)
		textBox.Text = "Correct code!"
		wait(1.5)
		textBox.TextColor3 = Color3.new(0,0,0)
		textBox.Text = " "
		event:FireServer()
	elseif textBox.Text ~= code then
		textBox.TextColor3 = Color3.new(255,0,0)
		textBox.Text = "Incorrect code!"
		wait(1.5)
		textBox.TextColor3 = Color3.new(0,0,0)
		textBox.Text = " "
	end
end
local function dataReturned()
	textBox.TextColor3 = Color3.new(255,0,0)
	textBox.Text = "Already entered code!"
	wait(1.5)
	textBox.TextColor3 = Color3.new(0,0,0)
	textBox.Text = " "
end
rep.ReturnData.OnClientEvent:Connect(dataReturned)
button.MouseButton1Click:Connect(readDataStore)

And your FINISHED



Any questions, don’t refrain from asking!
– NSG

2 Likes