So, I’m currently working on a twitter code system for my Roblox game and I know how to add codes and make it give the player the reward, however, what I am unclear about is how would I go about adding “Debounce” to the code so that the code can only be redeemed once.
Feel free to review my code, and please leave solutions in the replies, I’ll be ultimately grateful for them!
Script:
` local rep = game:GetService(“ReplicatedStorage”)
local o = rep.CodesEvent
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Enter.Text == “MIKEY” then
o:FireServer()
game.Players.LocalPlayer.leaderstats.Coins.Value = game.Players.LocalPlayer.leaderstats.Coins.Value + 10000
end
end) `
Try using a datastore. If they use the code, save it in a datastore, so when they enter the code again, they can’t use the code because it’s already been used.
You’ll need to utilize DataStores in order to save and access previously used codes, and you’ll need to compare the code the player is attempting to use to their data, to see if they’ve used it or not.
I’m also confused as to why you’re incrementing the player’s Coins value locally, as the server won’t read that change.
The method I personally chose to make this was utilizing a RemoteFunction. Basically, when I would press the ‘enter’ button to redeem the code, it’d invoke the RemoteFunction to the server, passing in the text inputted by the player. From there, you can set up a callback for the RemoteFunction, get the DataStore data for that player, and see if the code they’re attempting to redeem can be redeemed. I chose to use a RemoteFunction so it can return a message back to the client to display the state of the redemption. e.g: “Code redeemed!” or, “Already redeemed.”
In terms of actually storing player data, you can use something similar to this:
local ds = game:GetService("DataStoreService"):GetDataStore("Codes")
local used_codes = {}
game.Players.PlayerAdded:Connect(function(player)
local codes
pcall(function() codes = ds:GetAsync(player.UserId) end)
used_codes[player] = codes or {}
end)
game.Players.PlayerRemoving:Connect(function(player)
if used_codes[player] then
ds:SetAsync(player.UserId, used_codes[player])
end
end)
And server-sided when you go to redeem the code, you can read from that same table to determine if the inputted code exists or not.
local player_codes = used_codes[player] --//'player' being the player that's attempting to redeem the code
local input_code = input_msg:lower() --//Make it lowercase so they can't redeem it multiple times
if not player_codes[input_code] then --//Make sure code hasn't been redeemed already
player_codes[input_code] = true --//Write the new code in the player's used codes table to store that they already redeemed it
--//Give resources here
end
I hope this helps, this is an outline or a base for how I would personally go about this. Other people here might suggest alternative methods that are tidier.
Thank you for your detailed and accurate response, will definitely be trying this now, I’m a relatively new programmer and I know a lot of stuff but I get overwhelmed too and forget, once again thank you!