How to I insert an object into a table

So I’m trying to create a server-sided anti-cheat, but when trying to implement an anti-teleport system, I run into the following error:

I’m using this piece of code to insert the positions into the table when the player joins:

playerPositions[player.Name] = {
	['oldPos']=char:FindFirstChild('HumanoidRootPart').Position;
    ['newPos']=char:FindFirstChild('HumanoidRootPart').Position;
}
1 Like

Do you want to override an index? If so, you can do

playerPositions[player.Name]["newPos"] = --insert change

So I tried doing that in the player added event, but now I’m just getting the same error there

1 Like

Try printing out,

playerPositions[player.Name]

does it print out ‘nil’?

The code errors here


So nothing is printed

Could you show your whole code?

Vars:

playerPositions = {}
local checkRank = require(script.Parent.ProtonCommandSystem.checkPerms)

The playerAdded code:

game.Players.PlayerAdded:Connect(function(player)
	print('Test')
	player.CharacterAdded:Connect(function(char)
		playerPositions[player.Name]['oldPos']=char:FindFirstChild('HumanoidRootPart').Position;
		playerPositions[player.Name]['newPos']=char:FindFirstChild('HumanoidRootPart').Position;
		print(playerPositions[player.Name])
	end)
	
	player.CharacterRemoving:Connect(function(char)
		game.ServerStorage.ProtonFiles.antiCheatExemptPlayers:FindFirstChild(player.Name).Value = false	
	end)
end)

The loop checking for teleports:

while wait(10) do
	for i, player in pairs(game.Players:GetPlayers()) do
		print(playerPositions)
		--if checkRank(player, 250) == true or game.ServerStorage.ProtonFiles.antiCheatExemptPlayers:FindFirstChild(player.Name).Value == true then
		--	return
		--end
		local char = player.Character
		local rootPart: BasePart = char:WaitForChild('HumanoidRootPart')
		local oldPos = playerPositions[player.Name]['oldPos']
		local newPos = playerPositions[player.Name]['newPos']

		newPos = Vector3.new(rootPart.Position.X, 0, rootPart.Position.Z)
		
		if oldPos and newPos then
			print((oldPos-newPos).Magnitude)
			if (oldPos-newPos).Magnitude >= 1000 then
				player:Kick([[    
				Proton Anti-Exploit:
				Player Character Violation
				]])
			end
			
			oldPos = Vector3.new(rootPart.Position.X, 0, rootPart.Position.Z)
		end
	end
end
1 Like

Try this out:

-- playeradded code

game.Players.PlayerAdded:Connect(function(player)
	print('Test')
	player.CharacterAdded:Connect(function(char)
		playerPositions[player.Name] = {['oldPos'] =char:FindFirstChild('HumanoidRootPart').Position}
		playerPositions[player.Name] = {['newPos'] =char:FindFirstChild('HumanoidRootPart').Position}
		print(playerPositions[player.Name])
	end)

	player.CharacterRemoving:Connect(function(char)
		game.ServerStorage.ProtonFiles.antiCheatExemptPlayers:FindFirstChild(player.Name).Value = false	
	end)
end)
while wait(10) do
	for i, player in pairs(game.Players:GetPlayers()) do
		print(playerPositions)
		--if checkRank(player, 250) == true or game.ServerStorage.ProtonFiles.antiCheatExemptPlayers:FindFirstChild(player.Name).Value == true then
		--	return
		--end
		local char = player.Character
		local rootPart: BasePart = char:WaitForChild('HumanoidRootPart')

		playerPositions[player.Name]['newPos'] = Vector3.new(rootPart.Position.X, 0, rootPart.Position.Z)
		
		local oldPos = playerPositions[player.Name]['oldPos']
		local newPos = playerPositions[player.Name]['newPos']
		
		if oldPos and newPos then
			print((oldPos-newPos).Magnitude)
			if (oldPos-newPos).Magnitude >= 1000 then
				player:Kick([[    
				Proton Anti-Exploit:
				Player Character Violation
				]])
			end

			playerPositions[player.Name]['oldPos'] = Vector3.new(rootPart.Position.X, 0, rootPart.Position.Z)
		end
	end
end

That works for the most part, but ‘oldPos’ isn’t saved to the table

local oldPos = playerPositions[player.Name]['oldPos'] or Vector3.new(0, 0, 0)
local newPos = playerPositions[player.Name]['newPos'] or Vector3.new(0, 0, 0)

You need a default value for both variables, you could use Vector3.new(0, 0, 0).