Carrying data from a function in dialogue table

Not really sure how to title this, but basically I have a dialogue system and I want when the player sells their fish the dialogue to inform the player how much they sold their fish for. But as soon as I start the dialogue (and this module gets called) I get an error straight away on [3]. As FishSale is a nil value. So I thought easy solution, define FishSale at the top, and just have it as 0, and then obviously when the remote function returns something I’d get a result and while it didn’t error, it just returned 0.

Messages = {
	[1] = {Msg = "Hello!<AnimateYield=0.25> What can I help you with?", Type = 'Question', Choices = {['I want to sell my fish! 🐟'] = 2, ['I want to buy a new rod! 🎣'] = 5}},
			
	-- Sell fish
	[2] = function() 
		FishSale = SellFish:InvokeServer()
		print(FishSale) -- Prints 1
		return 3
	end,
	[3] = {Msg = "You have sold all your fish for <Color=75,211,60><Font=GothamBold>" .. FishSale .. '<Img=2283894564>', Type = 'Normal'},
	[4] = {Msg = "Pleasure doing business with you! 😊", Type = 'End'},
	
	-- Buy rods
	[5] = function() -- Buy rods
		print('Buy')
		
		return 6
	end,
	[6] = {Msg = "Thank you for stopping by!<AnimateYield=0.5> I look forward to seeing your face again 😊", Type = 'End'}
}

So FishSale returns 1 inside the function in [2]. However when it comes to dialogue in [3] it says 0.

Feel free to lemme know if this a bad dialogue tree system :grimacing: I’ve tried looking into other ways but this is the best I could find. Basic idea is it loops through each message in order, and so obviously first is [1]. It has a Type ‘Question’ which means player picks between [2] or [5]. [2] takes you to the [2] path, which then continues to [3]-[4] and ends there. And choosing [5] would go to [6] and end there

I’m using a RichText module (sorry can’t remember whose but ye :grimacing:)

Where have you defined FishSale as a local variable?

The issue is that FishSale is not defined, the best practise would probably be to make [3] a function which returns the Msg so that you can parse FishSale into it.

Something like

[3] = function(FishSale)
   return {Msg = "You have sold all your fish for <Color=75,211,60><Font=GothamBold>" .. FishSale .. '<Img=2283894564>', Type = 'Normal'}
end

I dont know if this will be possible in your usecase though

Another option is to (as rawbyte said), define FishSale as a local at the beginning of your code