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”
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]
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)