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

Uh, well I guess you could make it a boolvalue and put it in a datastore.

1 Like

Can u help me with that i am prettty new to scripting :confused:

Well, you need to make usage of a DataStore to save that they have redeemed it.
If you don’t care if it saves or not, then you can use the above example from @ShadowAlien98.

    local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Points")
     
    game.Players.PlayerAdded:Connect(function(player)
    	local playerKey = "Player_" .. player.UserId
    	-- Give 50 points to players each time they visit
    	local success, err = pcall(function()
    		pointsDataStore:UpdateAsync(playerKey, function(oldValue)
    			local newValue = oldValue or 0
    			newValue = newValue + 50
    			return newValue
    		end)
    	end)
    end)

This is an example of how to save something to a DataStore (found on the wiki)

Thats saving the data by every 50 points so if they dont get 50 points by the time they leave they would have nothing

(This is an example of tables)
Datastore Tutorial for Beginners - Resources / Community Tutorials - DevForum | Roblox
Wait…or is it dictionaries…

The code bit here tries to save.

local success, err = pcall(function()
	pointsDataStore:UpdateAsync(playerKey, function(oldValue)
		local newValue = oldValue or 0
		newValue = newValue + 50
		return newValue
	end)

It calls the pointsDataStore:UpdateAsync() function, but it has the argument of oldValue (which is the saved value in the DataStore), by default this is nil or non-existent.

local newValue = oldValue or 0
newValue = newValue + 50
return newValue

In this section, newValue is defined as the old value (could be nil and if it is, it will be set to 0). Then it increments whatever their oldValue is by 50.

So in layman terms, if you join with no data: it will do [0 + 50] else [your value + 50] so it just adds 50 to your current saved value.

Ohh so either way its gonna save no matter what your “POINTS” are ?

Correct, so in your case you could just save a boolean value (true or false) whether or not they have redeemed the code or not.

Ok but thats saving the points in general… i want it to save like the if you said that in chat like !code sin << example then you wont be able to get a reward for saying that twice or infinite times.

i just saw ur chat

The code:

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

Thats going to save if i leave?

That’s updating the values, to make the datastore:
(IDK if this works with bools but I think it does.)
Edit: Different scripts


local datakey = "ddtht664tge54"

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore(datakey)

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "stats"
	leaderstats.Parent = player
	
	local gold = Instance.new("BoolValue")
	gold.Name = "code"
	gold.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-code") 
	end)
	
	if success then 
		gold.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-code", player.leaderstats.code.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)

Thats gonna save this :

game.Players.PlayerAdded:Connect(function(player)
x=0
player.Chatted:Connect(function(msg)
if msg == string.lower(“!code sin”) and x == 0 then
x=1

		player.leaderstats.Points.Value = player.leaderstats.Points.Value + 40
		
	end
end)

end)

??

It will save the boolvalue. (If the player has used the code or not)

And that works with this script:

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

Do i put that DataStore on the same script?

Different scripts, one saves/loads the bool value (the long script), and the short one handles the thing that happens when the player uses the code.

So what if i wanna have multiple codes but keep the old once active?

Whats this

local datakey = “ddtht664tge54”