Dictionary That Manages Positions Turns Empty/Dictionary Issue/Bug

What Am I Trying To Achieve?
I am currently making a magic spell move that makes the caster recites a chant then teleport players around the caster into a dimension.It is a cutscene move,before the move finishes the caster and the players that been teleported into the dimension would be back at the position before the spell was casted.

Problem/Issue:
I created a few forum before this that is also about this one single same move.
First when the caster uses the move he recites a chant while players’ position that are around him will be placed into a dictionary.But the issue appears,my script for this cutscene is separated into sections,what I mean by that is like

Example:

game.ReplicatedStorage.ExampleRemote:Connect(function(player,section)
if section == “section1” then
–the first code that makes the table and insert all the players’s position around the caster into it
end

if section == “section2” then
–the line of code that needs the table from section 1 so it can teleports the players that are in the dictionary back to the position before the spell was casted.
end
end)
I couldnt reference the dictionary from section 1 to section 2 so I used the BindableFunction with the help of the last forum I posted.After I managed to get an answer from the forum and solve it the real problem comes in.Whenever I try printing the dictionary out it says nil and when the players that got teleport back by the section 2 code they always ended up in the wrong position that were once in before the spell was casted.The dictionary was completely empty???
I tried and fix but in the end I couldnt find out why.

Codes:
The code that creates the dictionary and stores the players’ position around the caster(including the caster position too)

Text:
local playerLocations = {}
for _,player in pairs(game.Players:GetPlayers()) do
if (player.Character.PrimaryPart.Position - player.Character.HumanoidRootPart.CFrame.Position).Magnitude < 100 then
playerLocations[player] = player.Character:GetPrimaryPartCFrame()

		end
	end
	script.Function:Invoke(playerLocations)

The code that teleports players back to the position before the spell was casted:

Text:
local caster = player.Name
local playerLocations = {}
script.Function.OnInvoke = function(t1)
playerLocations = t1
for _,player in pairs(game.Players:GetPlayers()) do
player.Character.HumanoidRootPart.Position = Vector3.new(playerLocations[player])
–playerLocations[player]
if player.Name == caster then return end
player.Character.Humanoid.Health = player.Character.Humanoid.Health - 80
end
end
end

end)

Video:(Lagged at the start for a few seconds)

Any help would be appreciated :heart: :happy3:

My guess is that you are using a mixed table. A mixed table is disallowed from being sent in its original form when using BindableEvents and Functions, as well as RemoteEvents and Functions. See about “Parameter Limitations” in this link: create.roblox.com/docs/reference/engine/classes/BindableEvent

You can do this, if this is what you are looking for?:

local yourTableHere = {}

game.ReplicatedStorage.ExampleRemote:Connect(function(player,section)
	if section == "section1" then
		yourTableHere[1] = "Here"
	end

	if section == "section2" then
		print(yourTableHere[1]) -- Output: Here
	end
end)
1 Like

Okay thanks,I manage to shorthen my code now but the problem is still there.
I still get teleported to the wrong place instead of the position where before the caster uses the spell.
I printed the player position which is
image

for _,player in pairs(game.Players:GetPlayers()) do
if (player.Character.PrimaryPart.Position - player.Character.HumanoidRootPart.CFrame.Position).Magnitude < 100 then
playerLocations[player] = player.Character:GetPrimaryPartCFrame()–doesnt this saves the position into the disctionary
print(player.Character:GetPrimaryPartCFrame().Position)
end
end

Is the playerLocations table still relying on the BindableEvent? Maybe try excluding the BindableEvent in the code and place the playerLocations at the start of the cutscene code?

1 Like

I completely remove the bindable lines of codes and did what you told me at the first post,I pasted the “local playerlocations = {}” at the top

image

Okay, found it. You were saving the entire CFrame values of players, then trying to turn it into a Vector3. This doesn’t convert a CFrame to a Vector3 position, but rather returns a zero-filled/empty Vector3 value.

Instead, you set the CFrame property of the HumanoidRootPart of players:

for _,player in game.Players:GetPlayers() do -- fun fact: no need for ipairs or pairs. just slap the table in the loop.
	player.Character.HumanoidRootPart.CFrame = playerLocations[player]
	
	-- oh yeah, if you don't need the CFrame values of the players after the cutscene ends, remove them:
	playerLocations[player] = nil
	
	if player.Name == caster then return end
		player.Character.Humanoid.Health = player.Character.Humanoid.Health - 80
	end
end

By the way, when writing posts, you can wrap code in two ```'s (tilde, left of the number 1 key) so that it is easier to read large blocks of code. You can also wrap one-line code in two single tildes, like this (`like this`)

1 Like

Thanks alot! Everything worked just like I wanted! :happy4:

1 Like