backward and forward ;)

public
vxpreen Jun 23, 2024 Never 105
Clone
Plaintext paste1.txt 82 lines (65 loc) | 3.26 KB
1
-- loadstring(game:HttpGet("https://pastecode.dev/raw/13imst2d/paste1.txt"))()
2
3
local player = game.Players.LocalPlayer
4
local userInputService = game:GetService("UserInputService")
5
local runService = game:GetService("RunService")
6
7
local toggled = false
8
local closestPlayer = nil
9
10
-- Function to find the nearest other player
11
local function findClosestPlayer()
12
local nearestDistance = math.huge
13
local closest = nil
14
local playerPosition = player.Character and player.Character:FindFirstChild("HumanoidRootPart").Position
15
16
if not playerPosition then return end
17
18
for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
19
if otherPlayer ~= player and otherPlayer.Character then
20
local character = otherPlayer.Character
21
local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
22
if humanoidRootPart then
23
local distance = (humanoidRootPart.Position - playerPosition).magnitude
24
if distance < nearestDistance then
25
nearestDistance = distance
26
closest = otherPlayer
27
end
28
end
29
end
30
end
31
32
return closest
33
end
34
35
-- Function to move forward and backward while looking at the closest player
36
local function moveForwardBackward()
37
if not toggled then return end
38
39
closestPlayer = findClosestPlayer()
40
if closestPlayer then
41
local character = player.Character
42
local humanoid = character and character:FindFirstChild("Humanoid")
43
local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
44
45
if humanoid and humanoidRootPart then
46
-- Determine direction towards the closest player
47
local direction = (closestPlayer.Character.HumanoidRootPart.Position - humanoidRootPart.Position).unit
48
49
-- Determine distance to the closest player
50
local distance = (closestPlayer.Character.HumanoidRootPart.Position - humanoidRootPart.Position).magnitude
51
52
-- Adjust movement based on distance
53
local movementDistance = 3
54
if distance <= 2 then
55
movementDistance = -3 -- Move backwards if within 2 studs
56
end
57
58
-- Calculate target position
59
local targetPosition = humanoidRootPart.Position + direction * movementDistance
60
61
-- Move to the target position
62
humanoid:MoveTo(targetPosition)
63
64
-- Look at the closest player without changing pitch (up and down)
65
local lookAtPosition = Vector3.new(closestPlayer.Character.HumanoidRootPart.Position.X, humanoidRootPart.Position.Y, closestPlayer.Character.HumanoidRootPart.Position.Z)
66
humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, lookAtPosition)
67
end
68
end
69
end
70
71
-- Toggle function
72
local function toggleMovement(input)
73
if input.KeyCode == Enum.KeyCode.Q then
74
toggled = not toggled
75
end
76
end
77
78
-- Connect the toggle function to user input
79
userInputService.InputBegan:Connect(toggleMovement)
80
81
-- Connect the movement function to the RenderStepped event
82
runService.RenderStepped:Connect(moveForwardBackward)