I'm trying to make a script to stop people from teleporting and I'm not getting any errors in Developer Console but its not working. Do you see anything wrong with the script?
01 | local plr = game.Players.LocalPlayer |
02 | local char = plr.Character |
03 |
04 | while wait() do |
05 | local rootpart = char:FindFirstChild( "HumanoidRootPart" ) |
06 | positionRecorder = { } |
07 | positionRecorder [ 1 ] = rootpart.Position |
08 | local pos 1 = positionRecorder [ 1 ] |
09 | wait( 0.1 ) |
10 | local pos 2 = rootpart.Position |
11 | local mag = (pos 1 - pos 2 ).Magnitude |
12 | if mag > 5 then |
13 | rootpart.CFrame = positionRecorder [ 1 ] |
14 | end |
15 | end |
Instead of recording the position of the rootpart, try to record the CFrame
of the rootpart because you are using the CFrame to set the position of character if the player teleports:
01 | local Players = game:GetService( "Players" ) |
02 | local player = Players.LocalPlayer |
03 |
04 | local function antiTP(char) |
05 | local posRecord = { } |
06 | while true do |
07 | table.insert(posRecord, 1 ,char:GetPrimaryPartCFrame) |
08 | local pos 1 = posRecord [ 1 ] .p --Returns Vector3 from CFrame by putting ".p" |
09 | wait(. 1 ) |
10 | local pos 2 = char:GetPrimaryPartCFrame.p |
11 | local mag = (pos 1 - pos 2 ).magnitude |
12 | if mag > 5 then |
13 | print ( "Teleport Detected!" ) |
14 | char:SetPrimaryPartCFrame(posRecord [ 1 ] ) |
15 | end |
16 | end |
17 | end |
18 |
19 | player.CharacterAdded:Connect(antiTP) |
Also, since you are going to need to use Vector3
to calculate the magnitude and CFrame
to actually set the character back to position before teleport, you will have to save vector3 for pos1 and CFrame for posRecord.