How to get the next set of data from 2 sets of values

Tails = {
		{Number = 1, Depth = 1, Cost = 0},
		{Number = 1, Depth = 2, Cost = 50},
		{Number = 1, Depth = 3, Cost = 100},
		{Number = 2, Depth = 1, Cost = 250},
		{Number = 2, Depth = 2, Cost = 500},
		{Number = 2, Depth = 3, Cost = 1000},
}

local PlayersTail, PlayersDepth = CheckTail:InvokeServer() -- This would return 1, 1 (Tail 1, Depth 1)

So from this I wanna be able to find out what’s next from this list, which in this case would be Number 1, Depth 2. And then once the player has Depth 3 for Number 1, it would move onto Number 2, Depth 1

when your invoking the server you wold need to pass along the current number and depth , and then you can do this:

local PlayersTail,PlayersDepth = CheckTail:InvokeServer(currentTail,currentDepth)
--then on the server you do this when the remote function is invoke
Tails = {
		{Number = 1, Depth = 1, Cost = 0},
		{Number = 1, Depth = 2, Cost = 50},
		{Number = 1, Depth = 3, Cost = 100},
		{Number = 2, Depth = 1, Cost = 250},
		{Number = 2, Depth = 2, Cost = 500},
		{Number = 2, Depth = 3, Cost = 1000},
}

local function GetNextTailInfo(player,currentTail,currentDepth)
    for index,tailInfo in pairs(Tails) do
       if tailInfo["Number"] == currentTail and tailInfo["Depth"] == currentDepth then
           return Tails[index+1]["Number"], Tails[index+1]["Depth"]
       end
    end
end
CheckTail.OnServerInvoke = GetNextTailInfo

--post Edited***
--warning there may be some syntax errors i did this online
1 Like

I just fixed something in the code in the original post

1 Like

Is there a reason all that has to be done on the server?? And if so, how can I return that info properly back to the client:

local PlayersTail, PlayersDepth = CheckTail:InvokeServer() -- If this returns the next tail

-- How can I print the cost of the next tail??
1 Like

no it doesnt i though you wanted to sorry you can just do this on the client then:

Tails = {
		{Number = 1, Depth = 1, Cost = 0},
		{Number = 1, Depth = 2, Cost = 50},
		{Number = 1, Depth = 3, Cost = 100},
		{Number = 2, Depth = 1, Cost = 250},
		{Number = 2, Depth = 2, Cost = 500},
		{Number = 2, Depth = 3, Cost = 1000},
}

local function GetNextTailInfo(currentTail,currentDepth)
    for index,tailInfo in pairs(Tails) do
       if tailInfo["Number"] == currentTail and tailInfo["Depth"] == currentDepth then
           return Tails[index+1]
       end
    end
end
--in thefunction above you just pass the currentTail and currentDepth to get the next one
--this function would return the table of information so you would use it like this
local currentTail,currentDepth = 1,1 -- just an example
local nextInfo =  GetNextTailInfo(currentTail,currentDepth)
print(nextInfo["Number"],nextInfo["Depth"],nextInfo["Cost"]
1 Like
local PlayersTail, PlayersDepth = CheckTail:InvokeServer()

local NextTail = GetNextTailInfo(PlayersTail, PlayersDepth)
print('Before', NextTail.Number, NextTail.Depth) -- Prints 1, 2 (correct)

local function GetNextTailInfo(tail, depth)
    for i, v in pairs(PurchaseData.Tails) do
       if v['Number'] == tail and v['Depth'] == depth then
           return PurchaseData.Tails[i + 1]
       end
    end
end

if BuyTail:InvokeServer() then
		PlayersTail, PlayersDepth = CheckTail:InvokeServer()
				
		NextTail = GetNextTailInfo(PlayersTail, PlayersDepth)
		print('Buying', NextTail.Number, NextTail.Depth) -- attempt to call nil value
end
1 Like

the function needs to come first like this:

local PlayersTail, PlayersDepth = CheckTail:InvokeServer()
local function GetNextTailInfo(tail, depth)
    for i, v in pairs(PurchaseData.Tails) do
       if v['Number'] == tail and v['Depth'] == depth then
           return PurchaseData.Tails[i + 1]
       end
    end
end

local NextTail = GetNextTailInfo(PlayersTail, PlayersDepth)
print('Before', NextTail.Number, NextTail.Depth) -- Prints 1, 2 (correct)

if BuyTail:InvokeServer() then
		PlayersTail, PlayersDepth = CheckTail:InvokeServer()
				
		NextTail = GetNextTailInfo(PlayersTail, PlayersDepth)
		print('Buying', NextTail.Number, NextTail.Depth) -- attempt to call nil value
end

Can i see your server side code for BuyTail and CheckTail

function BuyTail.OnServerInvoke(player)
	local User = PlayerData[player.UserId]
	if not User then return end
	
	local NextTail

	for i, v in pairs(PurchaseData.Tails) do
    	if v['Number'] == User.Tail and v['Depth'] == User.Depth then
    		NextTail = PurchaseData.Tails[i + 1]
        end
    end

	if User.Pearls >= NextTail.Cost then
		UpdatePearls(player, -NextTail.Cost, false)
		User.Tail = NextTail
		
		DataUpdated:FireClient(player, User)
		
		return true
	else
		return false
	end
end

If I understand correctly, your question is that you want to get the next Number when a player passes a certain depth correct?