Code redeeming system not working

I made a code redeeming system but it returns invalid code no matter what.
I tried looking for solutions but I couldn’t find one
I think it’s an issue with the table.find but I don’t know what else to use
Code:

local badgeCodeDictionary = {
	[661262958584740] = "OOPSsoSILLY",
	[3784587926611516] = "SOEVIL",
	[984651461585313] = "iHEARTducks"
}
local codes = {
	["RELEASE"] = 100,
	["OOPSsoSILLY"] = 50,
	["SOEVIL"] = 40,
	["iHEARTducks"] = 30
}

game.ReplicatedStorage.RedeemCode.OnServerInvoke = function(plr,code)
	
	if table.find(badgeCodeDictionary,code) then
		if game.BadgeService:UserHasBadgeAsync(plr.UserId,table.find(badgeCodeDictionary,code)) then
			plr.leaderstats.Coins += codes[code]
			return("Redeemed!")
		else
			return("You don't own the badge!")
		end
	elseif table.find(codes,code) then
	plr.leaderstats.Coins += codes[code]
	return("Redeemed!")
	else
		return("Invalid code!")
	end
end

The code on the client (if it helps):

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Text = "Redeeming.."
	local text = game.ReplicatedStorage.RedeemCode:InvokeServer(script.Parent.Parent.TextBox.Text)
	script.Parent.Text = text
	task.wait(3)
	script.Parent.Text = "Redeem"
end)

You should use indexing instead of table.find on dictionaries if you are checking keys and not values:

local badgeCodeDictionary = {
	[661262958584740] = "OOPSsoSILLY",
	[3784587926611516] = "SOEVIL",
	[984651461585313] = "iHEARTducks"
}
local codes = {
	["RELEASE"] = 100,
	["OOPSsoSILLY"] = 50,
	["SOEVIL"] = 40,
	["iHEARTducks"] = 30
}

game.ReplicatedStorage.RedeemCode.OnServerInvoke:Connect(function(plr, code)
	if badgeCodeDictionary[code] then
		if game.BadgeService:UserHasBadgeAsync(plr.UserId,badgeCodeDictionary[code]) then
			plr.leaderstats.Coins += codes[code]
			print("Redeemed!")
		else
			print("You don't own the badge!")
		end
	elseif codes[code] then
		plr.leaderstats.Coins += codes[code]
		print("Redeemed!")
	else
		print("Invalid code!")
	end
end)

I think the badge service doesnt work in Roblox Studio but i might be wrong idk. And also i want to add that you dont remove the codes from the table meaning they can be redeemed multiple times.

It does:

print(game:GetService("BadgeService"):UserHasBadgeAsync(game.Players.LocalPlayer.UserId, 2983459406401754)) -- The badge I got 15 mins ago from a game

image

They probably shouldn’t do that since it’s a server script, if they did other players in the server wouldn’t be able to redeem after one player did, if you’ve written that looking at my response, I forgot to delete what I was testing on a localscript since I didn’t want to deal with RemoteFunctions lol.

But the how will you stop player from ussing the code multiple times?

You can simply use tables but that wouldn’t save globally, so let’s say if the player left and joined another server they could be able to redeem the same code again. Use Data Stores instead to save data globally like redeemed codes and in-game earned currencies.

i totally forgot you can use variables for indexes, sigh

1 Like

i like to code things one at a time because then i have 12341234 bugs and this way theres not too many at once

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.