Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this youtube code datastore not working properly?

So I am using a datastore to determine if a player has redeemed a code or not. For some reason, even thought the value should be true after redeeming, it still allows them to redeem it multiple times.

Remote Event Script:

game.ReplicatedStorage.Remotes.GetYoutubeCodeInfo.OnServerInvoke = function(plr, typeOf, tag, data)
	local dts = game:GetService("DataStoreService")
	local localData = dts:GetDataStore("YoutubeCodes")

	
	if typeOf == "set" then
		pcall(function()
			localData:SetAsync(tag, data)
		end)
		
	elseif typeOf == "get" then
		local data
		
		pcall(function()
			data = localData:GetAsync(tag)
		end)
		
		if data ~= nil then
			if data.Sub2Hed ~= nil or data.Showcase ~= nil then
				data.Sub2Hed = nil
				data.Showcase = nil
				if data.YouTube == nil then
					data.YouTube = false
				
				elseif data.UpcomingGame == nil then
					data.UpcomingGame = false
				end
			end
			return data
		else
			local dataInfo = {}
			
			dataInfo.YouTube = false
			dataInfo.UpcomingGame = false
			
			localData:SetAsync(tag, dataInfo)
			
			return localData:GetAsync(tag)
		end
	end
end

Script that activates when the redeem button is clicked:

local codeList = {
	code1 = "YouTube",
	code2 = "UpcomingGame",
}

local Enter = script.Parent
local TypeCode = script.Parent.Parent.TypeCodeHere
local coins = game.Players.LocalPlayer:WaitForChild("Stats").Coins
local db = false
Enter.MouseButton1Click:Connect(function()
	if db == false then
	db = true
	local dataTable = game.ReplicatedStorage.Remotes.GetYoutubeCodeInfo:InvokeServer("get", game.Players.LocalPlayer.UserId)
	
	if TypeCode.Text == codeList.code1  then
		if dataTable.YouTube == false then
			TypeCode.Text = ""
			TypeCode.PlaceholderText = "Code redeemed succesfully!"
			game.ReplicatedStorage.Remotes.AddCoins:FireServer("coin", 1000, game.Players.LocalPlayer)
			local dataTableSend = {}
			dataTableSend.YouTube = true
			dataTableSend.UpcomingGame = dataTable.UpcomingGame
			game.ReplicatedStorage.Remotes.GetYoutubeCodeInfo:InvokeServer("set", game.Players.LocalPlayer.UserId.."-code", dataTableSend)
			wait(1.5)
			TypeCode.PlaceholderText = "Enter your code here"
			wait(3)
			db = false
		else
			TypeCode.Text = ""
			TypeCode.PlaceholderText = "Code has already been used!"
			wait(1.5)
			TypeCode.PlaceholderText = "Enter your code here"
			wait(3)
			db = false
		end
		
		
	elseif TypeCode.Text == codeList.code2 then
		if dataTable.UpcomingGame == false then
		TypeCode.Text = ""
		TypeCode.PlaceholderText = "Code redeemed succesfully!"
		game.ReplicatedStorage.Remotes.AddCoins:FireServer("coin", 500, game.Players.LocalPlayer)
		local dataTableSend = {}
		dataTableSend.YouTube = dataTable.YouTube
		dataTableSend.UpcomingGame = true
		game.ReplicatedStorage.Remotes.GetYoutubeCodeInfo:InvokeServer("set", game.Players.LocalPlayer.UserId.."-code", dataTableSend)
		wait(1.5)
		TypeCode.PlaceholderText = "Type your code here"
		wait(3)
		db = false
		
		else
			TypeCode.Text = ""
			TypeCode.PlaceholderText = "Code has already been used!"
			wait(1.5)
			TypeCode.PlaceholderText = "Enter your code here"
			wait(3)
			db = false
		end
		

		
		
	else
		TypeCode.Text = ""
		TypeCode.PlaceholderText = "Invalid code"
		wait(1.5)
		TypeCode.PlaceholderText = "Enter your code here"
		wait(3)
		db = false
	end
	
	else
		TypeCode.Text = ""
		TypeCode.PlaceholderText = "Please wait!"
		wait(1.5)
		TypeCode.PlaceholderText = "Enter your code here"
	end
end)

1 answer

The issue was I forgot to add the "-code" part to the key when I was getting the datastore.

This discussion is preserved as it was. New answers, comments, and votes are closed.