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)
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)
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.