Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Anti-Teleport script won't work and I don't know why?

Asked by 5 years ago

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?

01local plr = game.Players.LocalPlayer
02local char = plr.Character
03 
04while wait() do
05    local rootpart = char:FindFirstChild("HumanoidRootPart")
06    positionRecorder = {}
07    positionRecorder[1] = rootpart.Position
08    local pos1 = positionRecorder[1]
09    wait(0.1)
10    local pos2 = rootpart.Position
11    local mag = (pos1 - pos2).Magnitude
12    if mag >5 then
13        rootpart.CFrame = positionRecorder[1]
14    end
15end

2 answers

Log in to vote
0
Answered by
Yuuwa0519 197
5 years ago
Edited 5 years ago

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:

01local Players = game:GetService("Players")
02local player = Players.LocalPlayer
03 
04local function antiTP(char)
05    local posRecord = {}
06        while true do
07        table.insert(posRecord,1,char:GetPrimaryPartCFrame)
08        local pos1 = posRecord[1].p --Returns Vector3 from CFrame by putting ".p"
09        wait(.1)
10        local pos2 = char:GetPrimaryPartCFrame.p
11        local mag = (pos1 - pos2).magnitude
12        if mag > 5 then
13            print("Teleport Detected!")
14            char:SetPrimaryPartCFrame(posRecord[1])
15        end
16    end
17end
18 
19player.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.

Ad
Log in to vote
0
Answered by 5 years ago

Try to add a part to block the teleporter

Answer this question