How would send this table from one function to another?

I have two functions and I need to send this table to another function. How would I do this?

event.OnServerEvent:Connect(function(player)
	local teams = game:GetService('Teams')
	
	if player.Team == teams.Menu and player.Profile.Character.Value then -- make sure they are in the menu
		
		player.Team = teams.Playing -- make there team playing
		
		-- load in the player
		local spawnPoint = workspace.Spawns:GetChildren()[1]
		player.RespawnLocation = spawnPoint
		player:LoadCharacter()
		
		local lastChar = player.Character
		local lastCharPivot = lastChar:GetPivot()
		
		-- load the player in
		local char = player.Profile.Character.Value.Char:Clone()
		
		
		
		char.PrimaryPart.CFrame = lastChar.PrimaryPart.CFrame -- set primary cframe
		char.PrimaryPart.Anchored = false
		
		
		player.Character = char
		
		player.Character.Parent = workspace
		player.Character:PivotTo(lastCharPivot)
		lastChar:Destroy()
		
		local anims = loadAnims(player.Profile.Character.Value.Anims, player.Character.Humanoid) -- this is the table I need to send
		
		print('Character loaded')
		
	else
		
	end
end)

-- this is the second function in the same script
event.OnServerEvent:Connect(function(player,typeOf)
	print(typeOf)

	local currentChar = player.Profile.Character
	

	

	if currentChar.Value.Name == 'Gorilla' then
                      -- i need to get the table here
		local event2 = game:GetService('ReplicatedStorage'):FindFirstChild('AttackEvents'):FindFirstChild('Gorilla')

		event2:Fire(player,typeOf)
	end
end)


1 Like

You could either:
Return the table or
Set the table as a Local-Global Variable

3 Likes

When considering yes I could do this. BUT . . . Danggit there’s always a but isn’t there . . .
Anyhow this loads it fora certain player. If I were to have a global variable it would update that variable for every player therefore every player would have to use the same animations

I also cant return the table cause they are two separate function so I wont be able to get that table over to the second function

When I say Local-Global Variabke I mean it’s Global in the script, meaning it isn’t Global I’m for everything
And from what I know you can actually return the table

Ok how would I return the variable, cause the script is a server script so thats why I cant use a global variable cause then it will effect everyone

Not an expert with return so I might be astatine something dumb, but add at the end:

return (variableName)

Should work

You can create a global table which store keys that contain the ‘anims’ table for each player. This way:

local playerAnims = {} -- Initiates your global table here


event.OnServerEvent:Connect(function(player)
	local teams = game:GetService('Teams')

	if player.Team == teams.Menu and player.Profile.Character.Value then -- make sure they are in the menu

		player.Team = teams.Playing -- make there team playing

		-- load in the player
		local spawnPoint = workspace.Spawns:GetChildren()[1]
		player.RespawnLocation = spawnPoint
		player:LoadCharacter()

		local lastChar = player.Character
		local lastCharPivot = lastChar:GetPivot()

		-- load the player in
		local char = player.Profile.Character.Value.Char:Clone()



		char.PrimaryPart.CFrame = lastChar.PrimaryPart.CFrame -- set primary cframe
		char.PrimaryPart.Anchored = false


		player.Character = char

		player.Character.Parent = workspace
		player.Character:PivotTo(lastCharPivot)
		lastChar:Destroy()

		local anims = loadAnims(player.Profile.Character.Value.Anims, player.Character.Humanoid) -- this is the table I need to send
		
		playerAnims[player.UserId] = anims -- Adds a key to the table using the player's Id, which is unique to the player, then stores the 'anims' table in it
		
		print('Character loaded')

	else

	end
end)

-- this is the second function in the same script
event.OnServerEvent:Connect(function(player,typeOf)
	print(typeOf)

	local currentChar = player.Profile.Character




	if currentChar.Value.Name == 'Gorilla' then
		local anims = playerAnims[player.UserId] -- This gets the 'anims' table for your player
		local event2 = game:GetService('ReplicatedStorage'):FindFirstChild('AttackEvents'):FindFirstChild('Gorilla')

		event2:Fire(player,typeOf)
	end
end)

2 Likes

Best sollution, but add a PlayerRemoving event to clean that table so it does not get filled up forever causing memory leak

1 Like

Yes this should probably work I will try it out later!
And I will keep in mind what @Necro_las said.
Ill have to use this everytime the player dies and leaves.
Cause I reload the anims everytime they die! Thank you

I’ll make your answer the solution if it works

You only have to clear the player’s key when they leave the game, since as you reload them when they die, it will simply overwrite their key with the new anims.

Oh well that saves time.
Also on the subject, How would I get there key? I really always forget how to get something in a table lol

I mean the table that is set to there key(The anim table)

In this case ‘playerAnims[player.UserId]’ would return the animsTable stored inside the key ‘[player.UserId]’.

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