I need help on my Redeem Code Script

I need help on my redeem code script
this is the code:

local datastoreService = game:GetService("DataStoreService")
local codeStore = datastoreService:GetDataStore("CodeStore")
local httpService = game:GetService("HttpService")

local codes = {
	["100CREDITS"] =100
}

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.RedeemCode.OnServerEvent:Connect(function(plrTriggered,code)
		if plrTriggered == player then
			if codes[code] then
				if not table.find(codesRedeemed,code) then
					table.insert(codesRedeemed,code)
					player.leaderstats.Credits.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)

When I test it and enter my code it says “Redeem does not exist”


I don’t know how to fix it.
If you need more information to be able to help me just let me know.

1 Like

In the output, it shows Redeem does not exist are you passing the correct arguments to the server on the client? You might be doing: (on client)

Event:FireServer("Redeem", "TheCode")

Instead of:

Event:FireServer("TheCode")

Because the server only handles the plrTriggered arg (Player) and code arg (The code)

2 Likes

Please Specify which line, im a beginner at scripting so i dont know where to change the script.

1 Like

Can we see the local side of your script? It will help us understand what is being called.

2 Likes

just a heads up that causes a memory leak if not properly handled; move the redeem code OnServerEvent connection outside of the PlayerAdded connection; and remove the if plrTriggered == player then if statement; and then change the player.leaderstats.Credits.Value += codes[code] to plrTriggered.leaderstats.Credits.Value += codes[code]

2 Likes

Can you send the LocalScript that is firing the event?

1 Like

this is the only local script (idk if it’s firing the event) this is just to open the frame I think.( I don’t know much about scripting I watch YouTube tutorials, or read dev forum posts.)

local frame = script.Parent.Frame
local button = script.Parent.TextButton

button.MouseButton1Click:Connect(function()
	frame.Visible = not frame.Visible
end)

frame.TextButton.MouseButton1Click:Connect(function()
	local text = frame.TextButton.Text
	
	game.ReplicatedStorage.RedeemCode:FireServer(text)
end)

I hope someone can help :smiley:
I really want to add codes into my game!!!

why are you inputting the TextButton as the “code”? i suppose this is supposed to be inputting a TextBox's text?

1 Like

Thank you so much, I didn’t notice my typo. :blush:

1 Like

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