Trouble returning a value

Hey there! I am making a shop where, to buy something you have to invoke the server and it sends you a value back based on the player. It works, up until when the player does not have enough currency to buy the item. Now, there are 4 possibilities when someone buys something, the player would either already have the item equipped, owned the item but does not have it equipped, does not have the item but has enough currency to buy it, and does not have the item and does not have enough currency.
When I send the invoke server via the gui button
Code example:

--sends message to server to change trail

local trailName = script.Parent.Name
local originalText = script.Parent.Text

local player = game.Players.LocalPlayer
shopRemoteFunction = game.ReplicatedStorage:WaitForChild("ShopRemoteFunction")

function onClick()

	script.Parent.Text = "Processing..."

	local outputText = shopRemoteFunction:InvokeServer(trailName)
	print(outputText)
	script.Parent.Text = outputText
	if outputText == "Not Enough Points" or outputText == "Trail Already Equipped!" then
		script["Error Beep"]:Play()
	end
	if outputText == "Trail Successfully Purchased!" or outputText == "Trail Equipped" then
		script["Success!"]:Play()
	end
	wait(3)
	if outputText == "Trail Equipped" or  outputText == "Trail Purchased!" or outputText == "Trail Already Equipped!" then
		script.Parent.Text = "Trail Owned"
	else
		script.Parent.Text = originalText
	end
	

end
script.Parent.MouseButton1Down:Connect(onClick)

and the shop script receives it

--runs when a player purchases trail, changes it and subtracts points

local shopRemoteFunction = Instance.new("RemoteFunction")
shopRemoteFunction.Parent = game.ReplicatedStorage
shopRemoteFunction.Name = "ShopRemoteFunction"

--cost
local trailTable = {DefaultTrail = 0, GreenTrail = 50, BlueTrail = 100, YellowTrail = 250, OrangeTrail = 500, RedTrail = 1000, PurpleTrail = 2000}

--character.HumanoidRootPart:FindFirstChild(trailKey) == nil
function purchaseTrail(player, trailKey)
	
	local cost = trailTable[trailKey]
	local leaderstats = player:WaitForChild("leaderstats")
	local character = player.Character
	local bought = _G.boughtArray[player.userId]
	local same = (trailKey == player.PlayerTrail.Value)
	local alreadyBought = bought[trailKey] == trailKey
	--
	print(cost)
	print(character)
	print(bought)
	print(same)
	print(alreadyBought)
	if (not same) then
		print("FirstCheck")
			--check if it is not bought and can buy or we have already bought
			if ((not alreadyBought and leaderstats.Points.Value >= cost) or alreadyBought) then
				print("SecondCheck")
				--ruduce coins if we have not bought before
				if (not alreadyBought) then
					leaderstats.Points.Value = leaderstats.Points.Value - cost
					print("ThirdCheck")
				end
				
				--clone the item from local storage
				character.HumanoidRootPart:FindFirstChildOfClass("Trail"):Destroy()
			
				local trail = game.ReplicatedStorage[trailKey]
				local playerTrail = trail:Clone()
				playerTrail.Parent = character.HumanoidRootPart
				
				--check if item is tool and if not it is accessory
				player.PlayerTrail.Value = trailKey


				if character:FindFirstChild("UpperTorso") then
					playerTrail.Attachment0 = character.Head.FaceFrontAttachment
					playerTrail.Attachment1 = character.UpperTorso.WaistRigAttachment
				else
					playerTrail.Attachment0 = character.Head.FaceFrontAttachment
					playerTrail.Attachment1 = character.HumanoidRootPart.RootRigAttachment
				end
				
				--update bought array with new item
				_G.boughtArray[player.userId][trailKey] = trailKey
				
				if alreadyBought then
					print("It is 0")
					--return 0 is used to display an item we already purchased
					return "Trail Equipped"
				else
					print("It is 1")
					--return 1 is used to display a new purchase
					return "Trail Purchased!"
				end
				
				
				
			end
			
	elseif (same) then
		print("It is 2")
		--return 2 is used to display if it is an item we already have equipped
		return "Trail Already Equipped!"
		
	elseif (leaderstats.Points.Value < cost) then
		print("It is 3")
		--return 3 is used to pop up that we do not have enough money to purchase
		return "Not Enough Points"
	
	else
		print("Somethin Wrong Here....")	
		return "ERROR!"	
	end
	print("END!!!")
end

shopRemoteFunction.OnServerInvoke = purchaseTrail

It works nicely and returns when it is a 0, 1 or a 2. When it gets to 3 or when the player does not have enough points, in the gui button script, I get


I was at first thinking that it just was skipping the elseif

elseif (leaderstats.Points.Value < cost) then
		print("It is 3")
		--return 3 is used to pop up that we do not have enough money to purchase
		return "Not Enough Points"

and thinking that maybe one of the values wasn’t working right, but, it won’t return the else below it,

else
   	print("Somethin Wrong Here....")	
   	return "ERROR!"	
   end

which should return the “ERROR!” if there is any other possibility.
I am very unsure what is wrong or why it would say this, does anyone have any thoughts on this one?
Thanks!

1 Like

At the

if (not same) then

The condition is being met but the next if statement is not being met.
Since there is no else after that if statement, nil is returned.

--runs when a player purchases trail, changes it and subtracts points

local shopRemoteFunction = Instance.new("RemoteFunction")
shopRemoteFunction.Parent = game.ReplicatedStorage
shopRemoteFunction.Name = "ShopRemoteFunction"

--cost
local trailTable = {DefaultTrail = 0, GreenTrail = 50, BlueTrail = 100, YellowTrail = 250, OrangeTrail = 500, RedTrail = 1000, PurpleTrail = 2000}

--character.HumanoidRootPart:FindFirstChild(trailKey) == nil
function purchaseTrail(player, trailKey)

	local cost = trailTable[trailKey]
	local leaderstats = player:WaitForChild("leaderstats")
	local character = player.Character
	local bought = _G.boughtArray[player.userId]
	local same = (trailKey == player.PlayerTrail.Value)
	local alreadyBought = bought[trailKey] == trailKey
	--
	print(cost)
	print(character)
	print(bought)
	print(same)
	print(alreadyBought)
	if (not same) then
		print("FirstCheck")
		--check if it is not bought and can buy or we have already bought
		if ((not alreadyBought and leaderstats.Points.Value >= cost) or alreadyBought) then
			print("SecondCheck")
			--ruduce coins if we have not bought before
			if (not alreadyBought) then
				leaderstats.Points.Value = leaderstats.Points.Value - cost
				print("ThirdCheck")
			end

			--clone the item from local storage
			character.HumanoidRootPart:FindFirstChildOfClass("Trail"):Destroy()

			local trail = game.ReplicatedStorage[trailKey]
			local playerTrail = trail:Clone()
			playerTrail.Parent = character.HumanoidRootPart

			--check if item is tool and if not it is accessory
			player.PlayerTrail.Value = trailKey


			if character:FindFirstChild("UpperTorso") then
				playerTrail.Attachment0 = character.Head.FaceFrontAttachment
				playerTrail.Attachment1 = character.UpperTorso.WaistRigAttachment
			else
				playerTrail.Attachment0 = character.Head.FaceFrontAttachment
				playerTrail.Attachment1 = character.HumanoidRootPart.RootRigAttachment
			end

			--update bought array with new item
			_G.boughtArray[player.userId][trailKey] = trailKey

			if alreadyBought then
				print("It is 0")
				--return 0 is used to display an item we already purchased
				return "Trail Equipped"
			else
				print("It is 1")
				--return 1 is used to display a new purchase
				return "Trail Purchased!"
			end

		else
			
			return "Message Here"
			
		end

	elseif (same) then
		print("It is 2")
		--return 2 is used to display if it is an item we already have equipped
		return "Trail Already Equipped!"

	elseif (leaderstats.Points.Value < cost) then
		print("It is 3")
		--return 3 is used to pop up that we do not have enough money to purchase
		return "Not Enough Points"

	else
		print("Somethin Wrong Here....")	
		return "ERROR!"	
	end
	print("END!!!")
end

shopRemoteFunction.OnServerInvoke = purchaseTrail

Added a return “Message Here” of where you missed.

1 Like

Thanks it worked! I am not sure why because the other way works for other stuff I think, but anyway thanks again!