Function returns something only the first time but not returning anything every other time

I’m trying to make a shop, and the multiplier works, but the backpack part doesn’t. The backpack part only works the first time, but every other time, it doesn’t. Does anyone know how to fix this? Thanks.

Script:

--Multiplier
function Multiplier(player, cashInt)
	
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local multipler = player:WaitForChild("Upgrades"):WaitForChild("Multiplier")
	local price = multipler:WaitForChild("MultiplierPrice")
	

	if cashInt > cash.Value then
		return "Hack"
		
	elseif cash.Value >= price.Value then
		cash.Value -= price.Value
		local newPrice = math.floor(price.Value * 1.5)
		price.Value = newPrice
		multipler.Value = math.floor(multipler.Value * 1.05)
		
		return true
	else
		return false
	end

end





--Backpack
function BackAdd(player, cashInt)

	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local Backpack = player:WaitForChild("Upgrades"):WaitForChild("Backpack")
	local price = Backpack:WaitForChild("BackpackPrice")


	if cashInt ~= cash.Value then
		return "Hack"

	elseif cash.Value >= price.Value then

		if Backpack.Value > 0 then
			Backpack.Value += 2
			Backpack.Value -= price.Value
			local newPrice = math.floor(price.Value * 1.5)
			price.Value = newPrice

			return true
		end

	else

		return false
	end

end





function BackpackTimes(player, cashInt)

	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local Backpack = player:WaitForChild("Upgrades"):WaitForChild("Backpack")
	local price = Backpack:WaitForChild("BackpackPrice")


	if cashInt > cash.Value then
		return "Hack"

	elseif cash.Value >= price.Value then

		if Backpack.Value ~= 0 or Backpack.Value > 0 then
			Backpack.Value *= 2
			Backpack.Value -= price.Value
			local newPrice = math.floor(price.Value * 1.5)
			price.Value = newPrice
			return true

		end

	else
		return false
	end

end




local rfunc = game:GetService("ReplicatedStorage").Remotes.Upgrades.RemoteFunction

rfunc.OnServerInvoke = function(player, frame, cashInt, Button)
	local bought
	
	
	if frame == "Multiplier" then
		bought = Multiplier(player, cashInt)
	end

	
	if frame == "Backpack" and Button == "Add" then -- Add is add two slots
		bought = BackAdd(player, cashInt)
	elseif frame == "Backpack" and Button == "Multiply" then -- Multiply is multiply the slots by 2
		bought = BackpackTimes(player, cashInt)
	end
	
	
	return bought
end

Which function is only working once?

The backpack one is only working once

Also if anyone is wondering, my returning system goes like this:

true = The player bought it
false = The player does not have enough money for it
Hack = The player is hacking

BackpackTimes? What does it return the first time vs after that?

1 Like

BackAdd can return nil if Backpack.Value <= 0 removing the else might fix this as you’d like, same with BackpackTimes, they are set up to only return false if cash is less than the price.

function BackAdd(player, cashInt)

	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local Backpack = player:WaitForChild("Upgrades"):WaitForChild("Backpack")
	local price = Backpack:WaitForChild("BackpackPrice")


	if cashInt ~= cash.Value then
		return "Hack"

	elseif cash.Value >= price.Value then

		if Backpack.Value > 0 then
			Backpack.Value += 2
			Backpack.Value -= price.Value
			local newPrice = math.floor(price.Value * 1.5)
			price.Value = newPrice

			return true
		end
	end

	return false -- will always return false if nothing else
end

Oh, I’m testing BackAdd right now(BackpackAdd, I just changed it to something else because I thought it was the name that was causing the error.)

For BackAdd, it returns true the first time, and then every other time, it returns nil

BackAdd can only return nil if cash.Value >= price.Value but Backpack.Value is not greater than 0. So Backpack.Value is becoming negative somehow.

1 Like

You’re right, it was turning negative. I meant to do cash.Value -= price.Value instead of Backpack.Value -= price.Value. Thanks!

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