Simple remotefunction returning nil

Invoke:

function Inventory:Purchase(player,value, item, amt)
	local iPurchase = RS.InventoryEvents.iPurchase
	local data = Inventory:gSpend(value)
	print(data)
	if data == 0 then return 
	
	else
--rest of code here

Server side:

gSpend.OnServerInvoke = function(Player,value)
	
	local Profile = Manager.Profiles[Player]
	if not Profile then return end
	
	local gold = Profile.Data.Gold
	print(gold,value)
	
	if gold >= value then
		print("success")
		local success = 1
		return success
		
	else
		print("failure")
		local failure = 0
		return failure
	end
end

Console from server prints and client print:

07:26:21.960 700000 100000 - Server - CurrencyHandler:27
07:26:21.960 success - Server - CurrencyHandler:30
07:26:21.997 nil - Client - Inventory:373

Losing hair over this.

Me too, I figured out that they cannot return, usually, I fire back to the client, and wait for the event to fire on the client end with :Wait()

fr? I have a way crazier remote function that returns a complex table in table in table in table that works as intended but i can’t return a single integer??

1 Like

Thats what I figured out, sometimes this engine is really confusing

Where does the remote function actually get invoked?

local script in an image button under a shop frame. I right click the item to buy, it sends:

script.Parent.MouseButton2Click:Connect(function()
	invMod:Purchase(player, script.Parent.Value.Value, script.Parent.ItemNameText.Text, 1)
	task.wait()
	invMod:RefreshSlots(script.Parent.ItemNameText.Text)
end)

which goes to my inventory module script under my Inventory screenGUI as it handles all my inventory functions:

function Inventory:Purchase(player,value, item, amt)
	local iPurchase = RS.InventoryEvents.iPurchase
	local data = Inventory:gSpend(value)
	print(data)
	if data == 0 then return 
	
	else
	
		iPurchase:FireServer(item, amt, value)
		
		local gUpdate = RS.CurrencyEvents.gUpdate
		local data1 = gUpdate:InvokeServer(value)

		local function comma_value(amount)
			local formatted = amount
			while true do  
				formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
				if (k==0) then
					break
				end
			end
			return formatted
		end

		local formattedData = comma_value(data1)
		Player.PlayerGui.InventoryUI.PlayerInventory.Gold.Text = formattedData
	end
end

the local data == inventory:gspend(value) server checks the profiles gold to make sure they have enough to purchase the item:

function Inventory:gSpend(value)
	local gSpend = RS.CurrencyEvents.gSpend
	gSpend:InvokeServer(value)
end

server side gspend invoke (where failure is i presume somehow)

gSpend.OnServerInvoke = function(Player,value)

	local Profile = Manager.Profiles[Player]
	if not Profile then return end

	local gold = Profile.Data.Gold
	print(gold,value)

	local succorFail = {}

	if gold >= value then
		print("success")
		local success = 1
		table.insert(succorFail,success)
		return succorFail

	else
		print("failure")
		local failure = 0
		table.insert(succorFail,failure)
		return succorFail
	end
end

and then that allows the rest of the purchase mod above to continue, which all works fine because i check if the data variable == 0 and continue, and since its nil it continues and purchases the item just fine, but it will just go into the negative gold and allow them to purchase anyway since the check is failing and im not getting my integer returns.

uh ignore that last gspend i tried to make it a table return lol and it didnt work. reverted it:


gSpend.OnServerInvoke = function(Player,value)

	local Profile = Manager.Profiles[Player]
	if not Profile then return end

	local gold = Profile.Data.Gold
	print(gold,value)

	if gold >= value then
		print("success")
		local success = 1
		return success

	else
		print("failure")
		local failure = 0
		return failure
	end
end

Your not returning the data that was returned from the invoke here?

function Inventory:gSpend(value)
	local gSpend = RS.CurrencyEvents.gSpend
	gSpend:InvokeServer(value)
end

function Inventory:Purchase(player,value, item, amt)
	local iPurchase = RS.InventoryEvents.iPurchase
	local data = Inventory:gSpend(value)

This should still do that no?

function Inventory:gSpend(value)
	local gSpend = RS.CurrencyEvents.gSpend
	return gSpend:InvokeServer(value)
end

Try this instead

I’m going to cry… ty that worked… i’ve been on this for like 24 h. Building this inventory has me questioning life decisions. thank you so much ughhhhhhhhhh

1 Like

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