I recently watched a YouTube video on how to create a code redeemer script, I believe I wrote it up to par and checked to make sure it matched up and that I understood, however, the script does not work.
I added some debug, when I type in “RELEASE” and redeem it, the debug prints “Input code: nil” which means that it’s not finding the code correctly. Any help would be appreciated.
local DataStoreService = game:GetService("DataStoreService")
local CodeStore = DataStoreService:GetDataStore("CodeStore")
local HttpService = game:GetService("HttpService")
local codes = {
["RELEASE"] = 1000
}
game.Players.PlayerAdded:Connect(function(player)
local data = CodeStore:GetAsync(player.UserId)
local codesRedeemed
if data then
codesRedeemed = HttpService:JSONDecode(data)
else
codesRedeemed = {}
end
game.ReplicatedStorage.Events.RedeemCode.OnServerEvent:Connect(function(plrTriggered, code)
if plrTriggered == player then
print("Input code:", code)
if codesRedeemed[code] then
print("Codes redeemed:", table.concat(codesRedeemed, ", "))
if not table.find(codesRedeemed, code) then
table.insert(codesRedeemed, code)
player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + codes[code]
end
else
print("Code does not exist.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(plrLeaving)
if plrLeaving == player then
CodeStore:SetAsync(player.UserId, HttpService:JSONEncode(codesRedeemed))
end
end)
end)
local redeemButton = script.Parent.RedeemBtn
redeemButton.MouseButton1Click:Connect(function()
local text = script.Parent.CodeInput.Text
game.ReplicatedStorage.Events.RedeemCode:FireServer()
end)
You don’t have to, the function arguments come in pairs, when firing the function you need the same variable names as the variables as it would be nil otherwise but when declaring a function you can name the argument whatever you want as it just counts as a new variable
A bit confused, so when I fire RemoteEvent with the text variable, where would I put it inside of the Server-Side script? Would I write something like:
I am not sure if this is related to the issue but you should move the OnServerEvent and PlayerRemoving functions outside of the PlayerAdded function. If more than one player joins the server, your script will probably break because the event will fire multiple times.
game.Players.PlayerAdded:Connect(function(player)
local data = CodeStore:GetAsync(player.UserId)
local codesRedeemed
if data then
codesRedeemed = HttpService:JSONDecode(data)
else
codesRedeemed = {}
end
end
game.ReplicatedStorage.Events.RedeemCode.OnServerEvent:Connect(function(plrTriggered, code)
if plrTriggered == player then
print("Input code:", code)
if codesRedeemed[code] then
print("Codes redeemed:", table.concat(codesRedeemed, ", "))
if not table.find(codesRedeemed, code) then
table.insert(codesRedeemed, code)
player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + codes[code]
end
else
print("Code does not exist.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(plrLeaving)
if plrLeaving == player then
CodeStore:SetAsync(player.UserId, HttpService:JSONEncode(codesRedeemed))
end
end)
Edit: It looks like you will have to make some modifications to the script to get it to work outside of the PlayerAdded event
Sorry for the confusion you do not have to add a text variable to the OnServerEvent function the text variable will be passed as the code variable on the OnServerEvent function so you do not have to make any additions other than the one to the local script
Oh, I understand now. Never thought it worked like that.
So if you just fire a variable from a client script and then define another inside of the OnServerEvent bit it will treat the new variable (in this instance, code) as the text variable?
Yes, I know all of the explanations I gave are a bit confusing, here is the last one I hope;
A variable is just a memory key like a key to access the data so when you pass that variable all that happens is you pass the player as the first argument (Roblox does this automatically) then the data itself not the name or the memory key that’s how you can create a new variable because when the argument is passed to the function all it does is save that data to the memory under the memory key you specified when you declared the function