0% found this document useful (0 votes)
12 views2 pages

Rec

The script is designed for a Roblox game, where it identifies four specific goal parts in the workspace and allows the player to turn their character towards the second closest goal when the 'X' key is pressed. It utilizes various services such as UserInputService and VirtualInputManager to simulate mouse and keyboard inputs. The script includes functions for finding goal parts, calculating distances, and simulating character movement and camera adjustments.

Uploaded by

aprocertified
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Rec

The script is designed for a Roblox game, where it identifies four specific goal parts in the workspace and allows the player to turn their character towards the second closest goal when the 'X' key is pressed. It utilizes various services such as UserInputService and VirtualInputManager to simulate mouse and keyboard inputs. The script includes functions for finding goal parts, calculating distances, and simulating character movement and camera adjustments.

Uploaded by

aprocertified
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

local player = game.Players.

LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local userInputService = game:GetService("UserInputService")
local virtualInputManager = game:GetService("VirtualInputManager")

-- Store the 4 goal parts


local goalParts = {}

-- Find the 4 goal parts once at startup


local function findGoalParts()
for _, object in ipairs(workspace:GetDescendants()) do
if object:IsA("Part") and
(object.Size == Vector3.new(9.523109436035156, 0.0010000020265579224,
11.93341064453125) or
object.Size == Vector3.new(9.523109436035156, 0.0010000000474974513,
11.93341064453125)) then
-- Rename the part to "Goal"
object.Name = "Goal"
table.insert(goalParts, object)

-- If we found all 4 parts, break the loop


if #goalParts == 4 then
break
end
end
end
end

-- Get second closest goal


local function getSecondClosestGoal()
local characterPos = humanoidRootPart.Position
local distances = {}

-- Calculate distances for the 4 goals


for _, goal in ipairs(goalParts) do
table.insert(distances, {
part = goal,
distance = (characterPos - goal.Position).Magnitude
})
end

-- Sort by distance
table.sort(distances, function(a, b)
return a.distance < b.distance
end)

-- Return second closest


return distances[2] and distances[2].part
end

-- Simulate inputs function


local function simulateInputs()
local mouse = player:GetMouse()

-- Wait 0.01 sec after turning


task.wait(0.01)
-- Hold right mouse button (M2)
virtualInputManager:SendMouseButtonEvent(mouse.X, mouse.Y, 1, true, game, 0)

-- Wait 0.01 sec


task.wait(0.01)

-- Press R key
virtualInputManager:SendKeyEvent(true, "R", false, game)
task.wait(0.01)
virtualInputManager:SendKeyEvent(false, "R", false, game)

-- Wait 0.01 sec


task.wait(0.01)

-- Release right mouse button


virtualInputManager:SendMouseButtonEvent(mouse.X, mouse.Y, 1, false, game, 0)
end

-- Turn function
local function turnCharacter()
local secondClosestGoal = getSecondClosestGoal()
if secondClosestGoal then
local characterPos = humanoidRootPart.Position
local direction = (characterPos - secondClosestGoal.Position).Unit

-- Create a CFrame with the original direction


local originalCFrame = CFrame.new(characterPos, characterPos + direction)
-- Rotate the CFrame 14 degrees around the right vector (X-axis)
local rotatedCFrame = originalCFrame * CFrame.Angles(math.rad(14), 0, 0)

-- Set the new CFrame for the character and camera


humanoidRootPart.CFrame = rotatedCFrame
camera.CFrame = CFrame.new(camera.CFrame.Position) * rotatedCFrame -
rotatedCFrame.Position

-- Run the input simulation


task.spawn(simulateInputs)
end
end

-- Input handler
userInputService.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.X then
turnCharacter()
end
end)

-- Find the goals once at startup


findGoalParts()

You might also like