Adding Redeemable Codes in your Game
In this tutorial, we will be using a datastore to make it so that they are able to claim it only once, let’s start by adding the datastore variable.
local DataStore = game:GetService("DataStoreService"):GetDataStore("YOUR_DATASTORE_NAME")
We’ve added it now, this time you’d have to add a PlayerAdded Event and PlayerRemoving Event.
local DataStore = game:GetService("DataStoreService"):GetDataStore("YOUR_DATASTORE_NAME")
local codes = {}
game.Players.PlayerAdded:Connect(function(plr)
-- Codes
end)
game.Players.PlayerRemoving:Connect(function(plr)
-- Codes
end)
There’s a lot of way to save if the player already used the datastore but we will use one of the tricky method!
local DataStore = game:GetService("DataStoreService"):GetDataStore("YOUR_DATASTORE_NAME")
local data = {}
local codes = {
"Sample1",
"Sample2"
}
game.Players.PlayerAdded:Connect(function(plr)
data[plr.Name] = {}
local Data
local success, err = pcall(function()
Data = DataStore:GetAsync(plr.UserId)
end)
if success then
if Data ~= nil then
for i=1,#Data do
data[plr.Name][i] = Data[i]
end
end
end
while game.Players:FindFirstChild(plr.Name) do
for i=1,3 do
local success, err = pcall(function()
DataStore:SetAsync(plr.UserId, {unpack(data[plr.Name])})
end)
if success then
break
end
wait(1)
end
wait(120)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i=1,3 do
local success, err = pcall(function()
DataStore:SetAsync(plr.UserId, {unpack(data[plr.Name])})
end)
wait(1)
end
end)
You might have noticed I added a lot of things in the code, what I added are the followings:
- List item
- Tables that should contain all data and the codes table
- GetAsyncs/SetAsyncs with pcalls
- Saving Data when Leaving
- Auto-saves every 2 minutes
Now, we’re finish with saving the data, that should be good now, we will now detect if the player is chatting, we’re going to add more line codes in the PlayerAdded Event and put the Chatted Event.
local DataStore = game:GetService("DataStoreService"):GetDataStore("YOUR_DATASTORE_NAME")
local data = {}
local codes = {
"Sample1",
"Sample2"
}
game.Players.PlayerAdded:Connect(function(plr)
data[plr.Name] = {}
local Data
local success, err = pcall(function()
Data = DataStore:GetAsync(plr.UserId)
end)
if success then
if Data ~= nil then
for i=1,#Data do
data[plr.Name][i] = Data[i]
end
end
end
plr.Chatted:Connect(function(msg)
if string.sub(string.lower(msg),1,6) == "!code " then
local enteredcode = string.sub(msg,7)
if data[plr.Name] ~= nil then
for i=1,#data[plr.Name] do
if data[plr.Name][i] == enteredcode then
return
end
end
end
for i=1,#codes do
if codes[i] == enteredcode then
print("Code Given")
table.insert(data[plr.Name],codes[i])
print(data[plr.Name][1],data[plr.Name][2],data[plr.Name][3],data[plr.Name][4])
-- Add Reward
end
end
end
end)
while game.Players:FindFirstChild(plr.Name) do
for i=1,3 do
local success, err = pcall(function()
DataStore:SetAsync(plr.UserId, {unpack(data[plr.Name])})
end)
if success then
break
end
wait(1)
end
wait(120)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i=1,3 do
local success, err = pcall(function()
DataStore:SetAsync(plr.UserId, {unpack(data[plr.Name])})
end)
wait(1)
end
end)
You might have noticed I added a lot of things as well here, what I’ve added is just in the PlayerAdded Event and it’s this:
- List item
- Tables that should contain all data and the codes table
- GetAsyncs/SetAsyncs with pcalls
- Saving Data when Leaving
- Auto-saves every 2 minutes
That should be finished now, you can add your rewards in the comment of the script.
Now you can use the code only once, try it out and it will only print “Code Given” once every code!