DataStore2 script saving Bucks, but not saving Tickets when exchanging for Bucks

Hello,

In my game I have an exchange currency system. A player can exchange 1 Buck for 10 Tickets, vise versa. However, when a player tries to exchange 10 Tickets or more for 1 Buck or more, Tickets do not save, but Bucks do. In the Output Window, it says “player left, saved Currency”. Does anybody know why this is happening? I require the DataStore scripts with a main server script on server start.

Server script:

-- yes, i'll change this to use a math function later so we don't have to let roblox do decimal work
game.ReplicatedStorage:WaitForChild("Functions"):WaitForChild("ExchangeCurrency").OnServerInvoke = function(plr, exchangeType, exchangeFor, exchangeAmt)
		if plr:WaitForChild("Currency"):FindFirstChild(exchangeType) and plr:WaitForChild("Currency"):FindFirstChild(exchangeFor) then
			if exchangeType == "Tickets" and exchangeAmt ~= nil and exchangeAmt >= 10 and exchangeFor == "Bucks" then
				if plr.Currency[exchangeType].Value >= exchangeAmt then
					plr.Currency[exchangeType].Value = plr.Currency[exchangeType].Value - exchangeAmt
					plr.Currency[exchangeFor].Value = plr.Currency[exchangeFor].Value + exchangeAmt / 10
					return true
				end
			elseif exchangeType == "Bucks" and exchangeAmt ~= nil and exchangeAmt >= 1 and exchangeFor == "Tickets" then
				if plr.Currency[exchangeType].Value >= exchangeAmt then
					plr.Currency[exchangeType].Value = plr.Currency[exchangeType].Value - exchangeAmt
					plr.Currency[exchangeFor].Value = plr.Currency[exchangeFor].Value + exchangeAmt * 10
					return true
				end
			end
		else
			return false
		end
	end

Button code:

local ticketsTxt = script.Parent:WaitForChild("Tickets")
local bucksTxt = script.Parent:WaitForChild("Bucks")

local ticketsBtn = script.Parent:WaitForChild("TicketsExchange")
local bucksBtn = script.Parent:WaitForChild("BucksExchange")

local debounce = false

ticketsBtn.MouseButton1Click:Connect(function()
	if debounce == false then
		debounce = true
		
		local response = game.ReplicatedStorage:WaitForChild("Functions"):WaitForChild("ExchangeCurrency"):InvokeServer("Tickets", "Bucks", tonumber(ticketsTxt.Text))
		if response == true then
			ticketsBtn.Text = "Success!"
			wait(1)
			ticketsBtn.Text = "Exchange for Bucks"
			debounce = false
		else
			ticketsBtn.Text = "You don't have enough Tickets!"
			wait(3)
			ticketsBtn.Text = "Exchange for Bucks"
			debounce = false
		end
	end
end)

This probably won’t be the answer you want, but I’m pretty sure Datastore2 just isn’t very good. I’ve had multiple games have data issues with it before, and even normal datastore works better for me than Datastore2. If you’re really determined to use Datastore2, maybe somebody else can help.

You have to use :Increment() with DataStore2 when adding or subtracting a value from the player’s leaderstat or a NumberValue inside of the player.

1 Like