How can i make a code system like the video below?

Is that what i need to do for the script to work?

NAHH that didnt work… loll i am a little confused

2 things:

  1. If that did work, then the values would be global, for all players.
  2. The datastore part/section creates values inside of the player, then changes the values based on what’s been saved.

The server would have scope of that, yes. Only issue is you need each player to have their own values. This can be achieved with a PlayerAdded method, and luckily we already have one.

Something like this would work.

game.Players.PlayerAdded:Connect(function(player)
	
	local playerKey = "Player_" .. player.UserId
	
	local stats = Instance.new("Folder", workspace)
	stats.Name = player.Name .. "_stats"
	
	local code = Instance.new("BoolValue", stats)
	code.Name = "code"
	
	local codeb = Instance.new("BoolValue", stats)
	codeb.Name = "codeb"

	player.Chatted:Connect(function(msg)
		if msg == string.lower("!code sin") then
			local redeemed, firstTime = saveRedeemed(playerKey, "sin")
			code.Value = redeemed
			if firstTime ~= nil and firstTime == true then
				-- give them the reward (will save 1 time only)
				
			end
		elseif msg == string.lower("!code sin2") and codeb.Value == false then
			local redeemed, firstTime = saveRedeemed(playerKey, "sin2") 
			codeb.Value = redeemed
			if firstTime ~= nil and firstTime == true then
				-- give them the reward (will save 1 time only)
				
			end
		end
	end)
end)

Note, you can change where the values end up. Right now: when a player joins the game a folder named (in this example) “Trayeus_stats” is created in the Workspace and it has their stats.

Its give me an error with saveRedeemed

The full code would be:

local codesDataStore = game:GetService("DataStoreService"):GetDataStore("Codes")
     
function saveRedeemed(playerKey, code)
	local redeemed = false
	local firstTime = false
	local success, err = pcall(function()
		codesDataStore:UpdateAsync(playerKey .. "_" .. code, function(oldValue)
			local newValue = oldValue or false
			if newValue == false then
				newValue = true -- new save value
				redeemed = true
				firstTime = true
			end
			return newValue
		end)
	end)
	if not success then -- fail safe
		redeemed = true
	end
	return redeemed, firstTime
end

game.Players.PlayerAdded:Connect(function(player)
	
	local playerKey = "Player_" .. player.UserId
	
	local stats = Instance.new("Folder", workspace)
	stats.Name = player.Name .. "_stats"
	
	local code = Instance.new("BoolValue", stats)
	code.Name = "code"
	
	local codeb = Instance.new("BoolValue", stats)
	codeb.Name = "codeb"

	player.Chatted:Connect(function(msg)
		if msg == string.lower("!code sin") then
			local redeemed, firstTime = saveRedeemed(playerKey, "sin")
			code.Value = redeemed
			if firstTime ~= nil and firstTime == true then
				-- give them the reward (will save 1 time only)
				
			end
		elseif msg == string.lower("!code sin2") and codeb.Value == false then
			local redeemed, firstTime = saveRedeemed(playerKey, "sin2") 
			codeb.Value = redeemed
			if firstTime ~= nil and firstTime == true then
				-- give them the reward (will save 1 time only)
				
			end
		end
	end)
end)

The script is good but its still not giving me the stats points

local codesDataStore = game:GetService(“DataStoreService”):GetDataStore(“Codes”)

function saveRedeemed(playerKey, code)
local redeemed = false
local firstTime = false
local success, err = pcall(function()
codesDataStore:UpdateAsync(playerKey … “_” … code, function(oldValue)
local newValue = oldValue or false
if newValue == false then
newValue = true – new save value
redeemed = true
firstTime = true
end
return newValue
end)
end)
if not success then – fail safe
redeemed = true
end
return redeemed, firstTime
end

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

local playerKey = "Player_" .. player.UserId

local stats = Instance.new("Folder", workspace)
stats.Name = player.Name .. "_stats"

local code = Instance.new("BoolValue", stats)
code.Name = "code"

local codeb = Instance.new("BoolValue", stats)
codeb.Name = "codeb"

player.Chatted:Connect(function(msg)
	if msg == string.lower("!code sin") then
		local redeemed, firstTime = saveRedeemed(playerKey, "sin")
		code.Value = redeemed
		if firstTime ~= nil and firstTime == true then
			player.leaderstats.Points.Value = player.leaderstats.Points.Value + 40 -- give them the reward (will save 1 time only)

		end
	elseif msg == string.lower("!code sin2") and codeb.Value == false then
		local redeemed, firstTime = saveRedeemed(playerKey, "sin2") 
		codeb.Value = redeemed
		if firstTime ~= nil and firstTime == true then
			player.leaderstats.Points.Value = player.leaderstats.Points.Value + 40 -- give them the reward (will save 1 time only)

		end
	end
end)

end)

So the value, inside the datastore would be for example:
Player_352463_codeb
?

It works for me, but it does give this warning:

  ▶ DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 522562539 (x2)  -  Studio
local code1 = {}
code1.Code = "yeet"
code1.Name = "YEET"
code1.Type = "Coins"
code1.Amount = 100
local code2 = {}
code2.Code = "omgvisits"
code2.Name = "YEETSAUCE"
code2.Type = "Coins"
code2.Amount = 200
local codes = {code1,code2}
game.Players.PlayerAdded:Connect(function(p)
	p.Chatted:Connect(function(msg)
		if string.match(msg, "!code") then
			for i,v in pairs(codes) do
				if string.match(msg, v.Code) then
					print(p.Name.." redeemed code "..v.Name.." for "..tostring(v.Amount).." "..v.Type)
				end
			end
		end
	end)
end)

This is just an example of an easy to add codes to system. You need to write your own code to learn and we can not hand out scripts on the DevForum and this needs to be made SPECIALLY made for your game

The script works for u?? what how

Was your game published with the datastore script?

This line should look something like:

Points.Value = Points.Value + 40

So, you would need to add a Points value as well like:
(this is part of the code):

local stats = Instance.new("Folder", workspace)
stats.Name = player.Name .. "_stats"

local Points = Instance.new("NumberValue", stats)
Points.Name = "Points"

local code = Instance.new("BoolValue", stats)
code.Name = "code"

local codeb = Instance.new("BoolValue", stats)
codeb.Name = "codeb"

ummm what do you mean??? I dont understand

I just added a random saving leaderboard for the points system:

code test - Roblox
BTW, API services/requests and or HTTP requests need to be on.

1 Like

What would be an example of the value, thats saved in the true/false thing, like the datastore is codes, what’s an example of a value in it?

Guys it worked… thank yalll!!

1 Like

Shadow can i give u some robux for helping aswell because i can only put one solution…

I feel like that’s against TOS cuz…it’s roblox… also me learning more about datastores is enough, it wasn’t super complicated anyways.