So, Im making a code (Twitter) GUI and I can input the codes and it checks whether if its invalid or not and it will say “Successfully Redeemed” when the code is correct. But, the rewards will not be given to the player. (I have read through the code multiple times and I still cannot figure out what is wrong.)
Here is my Serverscript that handles the datastore of codes
local ds = game:GetService("DataStoreService"):GetDataStore("Codes01")
game.Players.PlayerAdded:Connect(function(player)
local key = "codes-"..player.userId
local folder = Instance.new("Folder",player)
folder.Name = "Codes"
local save = ds:GetAsync(key)
if save then
for i = 1,#save do
local temp = Instance.new("BoolValue",folder)
temp.Name = save[i]
end
end
end)
game.ReplicatedStorage.EnterCode.OnServerEvent:Connect(function(player,reward,code)
local key = "codes-"..player.userId
local currency = player.leaderstats:FindFirstChild("Bacons")
currency.Value = currency.Value + reward
local bool = Instance.new("BoolValue",player.Codes)
bool.Name = code
bool.Value = true
local activated = {}
for i,v in pairs(player.Codes:GetChildren()) do
if v:isA("BoolValue") then
table.insert(activated, v.Name)
ds:SetAsync(key,activated)
end
end
end)
Here is my local script that handles code input
local player = game.Players.LocalPlayer
local RE = game.ReplicatedStorage.EnterCode
local codes = player:WaitForChild("Codes")
local codelist = {
code1 = "Reset",
code2 = "Welcome",
}
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Parent.Parent.Parent.CodesGUI.CodeFrame.TypeFrame.CodeHandler.Text == codelist.code1 then
if not codes:FindFirstChild("Code1") then
RE:FireServer(100, "Code1")
script.Parent.Text = "Code redeemed successfully!"
script.Parent.Parent.Parent.Parent.Parent.CodesGUI.CodeFrame.TypeFrame.CodeHandler.Text = ""
wait(2)
script.Parent.Text = "Redeem Code"
else
script.Parent.Text = "Code already redeemed!"
wait(2)
script.Parent.Text = "Redeem Code"
end
else
script.Parent.Text = "Code expired!"
wait(2)
script.Parent.Text = "Redeem Code"
end
if script.Parent.Parent.Parent.Parent.Parent.CodesGUI.CodeFrame.TypeFrame.CodeHandler.Text == codelist.code2 then
if not codes:FindFirstChild("Code2") then
RE:FireServer(10, "Code2")
script.Parent.Text = "Code redeemed successfully!"
script.Parent.Parent.Parent.Parent.Parent.CodesGUI.CodeFrame.TypeFrame.CodeHandler.Text = ""
wait(2)
script.Parent.Text = "Redeem Code"
else
script.Parent.Text = "Code already redeemed!"
wait(2)
script.Parent.Text = "Redeem Code"
end
else
script.Parent.Text = "Code invalid!"
script.Parent.Parent.Parent.Parent.Parent.CodesGUI.CodeFrame.TypeFrame.CodeHandler.Text = ""
wait(2)
script.Parent.Text = "Redeem Code"
end
end)
Oh yeah, sometimes this warning will pop out saying that the datastore is sending too many requests.
Can you guys help me check if there is anything wrong with it. Thank you!