Help with tables

I’m making a cutscene system, cutscene system supports multiple players depending on how many player boxes i have set up

function CutsceneModule.Play(Characters)
	local CharacterPositions = {}
	local playercount = #Characters



	for _, descendant in ipairs(script.Parent:GetDescendants()) do
		if descendant:IsA("Part") and descendant.Name == "CharacterPosition" then -- get character positions and add to table for multiple player support
			table.insert(CharacterPositions, descendant)
		end
	end	

	for _, Char in ipairs(Characters) do
		for _, CharPos in ipairs(CharacterPositions) do
			local Animation = CharPos.AnimationStuff.Animation
			local VFX = CharPos.AnimationStuff.VFX	
			Char.HumanoidRootPart.Position = CharPos.Position		
			Char.HumanoidRootPart.Orientation = CharPos.Orientation		
		end
	end
end

running this puts both players in the same position, I know why i just dont know how to fix it, im not used to using tables.

1 Like

Your code is kindof complicated, i’ll show example soo you can create new one from it

local Players = {}
local function GetPlayers()
    for _, player in game:GetService("Players"):GetChildren() do
        table.insert(Players, player) -- we insert player to our Players table
    end
end

local function TeleportPlayersToCutscenePosition(CutsceneStart: Vector3)
    for _, player in Players do
        local offset = Vector3.new(_ * 4, 0, 0) -- example offset
        player.Character:SetPrimaryPartCFrame(CFrame.new(CutsceneStart + offset))
        --# it should place players in line starting from CutsceneStart vector
    end
end

GetPlayers()
TeleportPlayersToCutscenePosition(workspace.Cutscene1.Position)

EDIT: Remember to run:

table.clear(Players)

to prevent memory leaks!

This is not a viable solution to my problem i think, wouldn’t this just do the same thing if i added my code back into it?

I believe this is occurring because of the final loops in the function:

From my observations, it appears that for every Character sent to the function, it’s looping through everything that was stored inside of the CharacterPositions table.

If each Character is only meant to be assigned one value from the table, you could instead make sure the indexes from each loop match up before updating the positions to ensure that it selects a unique / separate “CharacterPosition” part from the table compared to the previous Character model.

Example Revision

    for positionInLoop, Char in ipairs(Characters) do
		for numberOfBox, CharPos in ipairs(CharacterPositions) do
            if positionInLoop == numberOfBox then
			    local Animation = CharPos.AnimationStuff.Animation
			    local VFX = CharPos.AnimationStuff.VFX	
			    Char.HumanoidRootPart.Position = CharPos.Position		
			    Char.HumanoidRootPart.Orientation = CharPos.Orientation	
            end
		end
	end
1 Like

Thank you, works great, also, very well formatted post.

1 Like

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