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”
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 the datakey is just the datastore.
Does code = player.stats.code?
Here is a prototype script. I have not tested it, so let me know how it works. It should save and only let you redeem once.
*removed check farther down*
edited the code a bit
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
player.leaderstats.Points.Value = player.leaderstats.Points.Value + 40
end
end)
end)
Try his, I’m starting to confuse myself lol.
My bad, I forgot to change the codesDataStore
variable naming! Try this?
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
player.Chatted:Connect(function(msg)
if msg == string.lower("!code sin") then
-- do stuff with msg and player
local redeemed, firstTime = saveRedeemed(playerKey, "sin") -- make sure if you add more codes you change the if statement AND this
if firstTime ~= nil and firstTime == true then
-- give them the reward (will save 1 time only)
end
end
end)
end)
Thats wierd it isnt giving me any errors but its not giving me the points?
Am I supposed to put player.leaderstats.Points.Value = player.leaderstats.Points.Value + 40
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
player.Chatted:Connect(function(msg)
if msg == string.lower("!code sin") then
-- do stuff with msg and player ***<<<< Here***
local redeemed, firstTime = saveRedeemed(playerKey, "sin") -- make sure if you add more codes you change the if statement AND this
if firstTime ~= nil and firstTime == true then
– give them the reward (will save 1 time only) <<<< Or Here