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

You would do it under – give them the reward (will save 1 time only) line, yes.

Bare in mind this needs to be a server script as DataStores are ONLY accessible by the server.

The issue would be that you can’t access player.leaderstats.Points.Value on the Server… you would need to do this on the server instead.

I am putting this in a server script inside serverscriptservice

Whats this for then

– do stuff with msg and player

when i put it in where it says – do stuff with msg and player
it actually gives me the points but it doesnt save

That should be fine, yes. Also bare in mind this will only work once (as it saves). So to test or reset ALL player data you can change this line:

local codesDataStore = game:GetService("DataStoreService"):GetDataStore("Codes")

You would edit “Codes” to like “Codes001” or “Codes002” etc.

Also, that line – do stuff with msg and player is a comment. You can just remove it and put what you want to happen there.

And if you want to edit datastores and don’t want to buy the other editor use this:
[OPEN SOURCE] Free DataStore Editor Plugin! - Resources / Community Resources - DevForum | Roblox

2 Likes

@ShadowAlien98 makes a better point. You could just delete/edit YOUR save data to test!

So do i just put this

game.Players.PlayerAdded:Connect(function(player)
code = player.stats.code
codeb = player.stats.codeb
player.Chatted:Connect(function(msg)
if msg == string.lower("!code sin") and code.Value == false then
code.Value = true
elseif msg == string.lower("!code sin2") and codeb.Value == false then
codeb.Value = true
– do stuff with msg and player
end
end)
end)

and then use the plugin

Its asking me for a datastore name what do i put?

Use the plugin to edit the values, and if you want an example I made this:

code test - Roblox
(Should be uncopylocked.)

But i dont understand what do i put for name?

The datastore, in my game it’s “ddtht664tge54”

function saveRedeemed(playerKey, code)
	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
				firstTime = true
			end
			return newValue
		end)
	end)
	return firstTime
end

This function is what saves AND checks if you have already saved. So it will be vitally important for what you are wanting to do.
You can do something like this, however:

local codesDataStore = game:GetService("DataStoreService"):GetDataStore("Codes")
     
function saveRedeemed(playerKey, code)
	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
				firstTime = true
			end
			return newValue
		end)
	end)
	return firstTime
end

game.Players.PlayerAdded:Connect(function(player)
	
	local playerKey = "Player_" .. player.UserId
	
	local code = player.stats.code
	local codeb = player.stats.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)

You can name the DataStore anything you’d like! In my example, it is “Codes”. It can be whatever, it is just an identifier for the DataStore.

Oh i didnt know that… loll

and btw its still not giving me the points idk why

Any errors? If not then try reseting the datastore.

Its saying that this is not in my character wy do i need this in the script

local code = player.stats.code
local codeb = player.stats.codeb

I have to have a leaderstats name code and codeb?

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 code = player.stats.code
	local codeb = player.stats.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)

I think I found an error in my logic… this may work better.
***I edited the saveRedeemed() function

I just used your example, like stated before, you cannot access Client side data (in this case anything that is a descendant of Player) is not replicated to the server – so it cannot access it.

Check this from earlier: