Server sided teleport anti-cheat doesn't work the way it is supposed to

I want to achieve a working teleport anit-cheat script that is server sided with Magnitude, the issue with this is that this is my first time working with Magnitude and it keeps kicking me if I move to far away from the spawn point, i know why it keeps kicking me though if i move to far away from the spawn point, it is because it sets the first position when you spawn into the game, but i haven’t found any solution for this, i’ve tried already setting the FirstPositon new every 2.5 seconds, but this didn’t help at all because it stopped kicking me.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Character = Player.CharacterAdded:Wait()
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local FirstPosition = HumanoidRootPart.Position
	
	while wait() do
		local SecondPosition = HumanoidRootPart.Position
		if(FirstPosition - SecondPosition).Magnitude >= 60 then
			Player:Kick("\n\n[Kicked by: Server]\n[Reason: Suspicious actions]\n")
		end
	end
end)
1 Like

I recommend not kicking the person and just teleporting them back to their original location because kicking might result in kicking someone who just has super high ping., Also walkspeed is basically equal to studs so instead of asking if its greater or equal to 60 I would ask if it is greater or equal to walkspeed + 20. And then use a while wait(1) do then and set the FirstPosition to the players position at the end of the while wait. Something like this but I recommend rewriting it because this isn’t perfect.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		wait(1)
		local Humanoid = Character:WaitForChild("Humanoid")
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		local FirstPosition = HumanoidRootPart.CFrame
		while wait(1) do
			if(FirstPosition.p - HumanoidRootPart.CFrame.p).Magnitude >= Humanoid.WalkSpeed + 15 then
				HumanoidRootPart.CFrame = FirstPosition
			end
			FirstPosition = HumanoidRootPart.CFrame
		end
	end)
end)

Well, this seems to work but it keeps teleporting me up into the air after i spawn into the game

That’s why I said you need to rewrite it.Simply just teleport the player after the

local HumanoidRootPart = Character:WaitForChild(“HumanoidRootPart”)

so if we want the spawn to be at 100,0,0 just CFrame the HumanoidRootPart to that CFrame. Then after you teleported the player set their FirstPosition to be equal to the Current HumanoidRootPart CFrame.

1 Like

You should update the first position in the loop, wait, update the second position and check their difference. The way you have it setup now the player won’t be able to walk further than 60 studs from where they initially spawned.