can you generate me a lua script that will make altplayer1 match position and rotation of specifiedPlayer but also when being 5 studs more down and rotated by 90 degrees downwards and also theres gonna be altplayer2 that will match torso of altplayer1 while being 4 stud more up relative to altplayer2 and also theres gonna be altplayer3 that will do the same as altplayer2 but instead of 5 studs up its 5 studs down?
Assistant
Certainly! Below is a Lua script based on your description. This assumes you are working in Roblox (because of references like Studs, players, torso, positions, rotations, etc.).
Requirements Recap:
altplayer1 should match the position and rotation of specifiedPlayer, but:
5 studs lower (down)
rotated 90 degrees downwards (which we can interpret as rotating around the X-axis by +90 degrees).
altplayer2 should match the torso of altplayer1, but 4 studs higher relative to altplayer2's orientation.
altplayer3 should do the same as altplayer2 but 5 studs lower relative to altplayer3's orientation.
Assumptions:
Each player has a Character model, with relevant parts:
For specifiedPlayer and altplayer1: we use HumanoidRootPart for position/rotation control.
altplayer2 and altplayer3 are also characters and have a 'Torso' (usually "UpperTorso" or "Torso" depending on rig).
Rotation down 90 degrees = rotating along the X-axis by 90 degrees (pitch).
The script will be updating these every frame in a RunService.RenderStepped or RunService.Heartbeat loop.
The players are accessible with variables altplayer1, altplayer2, altplayer3, and specifiedPlayer.
The script
lua
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Set player references here:
local specifiedPlayer = Players:FindFirstChild("specifiedPlayerName")
local altplayer1 = Players:FindFirstChild("altplayer1Name")
local altplayer2 = Players:FindFirstChild("altplayer2Name")
local altplayer3 = Players:FindFirstChild("altplayer3Name")
if not (specifiedPlayer and altplayer1 and altplayer2 and altplayer3) then
warn("One or more players not found.")
return
end
-- Utility function to get torso part name depending on rig type
local function getTorso(character)
return character:FindFirstChild("HumanoidRootPart") or character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
end
local function getRotationDown90()
-- Create a CFrame that represents 90 degrees downward rotation around X axis
-- Angle in radians:
local angle = math.rad(90)
return CFrame.Angles(angle, 0, 0)
end
RunService.Heartbeat:Connect(function()
-- Get parts
local specifiedHRP = getTorso(specifiedPlayer.Character)
local alt1HRP = getTorso(altplayer1.Character)
local alt2Torso = getTorso(altplayer2.Character)
local alt3Torso = getTorso(altplayer3.Character)
if not (specifiedHRP and alt1HRP and alt2Torso and alt3Torso) then
-- One or more parts missing; skip update this frame
return
end
-- Step 1: altplayer1 matches position and rotation of specified player,
-- but 5 studs down and rotated 90 degrees downward.
-- 5 studs down means subtract 5 studs in Y axis (world space)
-- Let's do:
-- Base CFrame = specifiedHRP.CFrame
-- RotationDown = rotate 90 degrees downward around X axis
-- Position adjusted by moving 5 studs down (in world Y)
local baseCFrame = specifiedHRP.CFrame
local rotatedCFrame = baseCFrame * getRotationDown90()
local adjustedPosition = rotatedCFrame.Position - Vector3.new(0,5,0)
local newCFrame1 = CFrame.new(adjustedPosition) * (rotatedCFrame - rotatedCFrame.Position) -- keep rotation, apply new position
-- Apply to altplayer1's HRP
alt1HRP.CFrame = newCFrame1
-- Step 2: altplayer2 should match the torso of altplayer1 but 4 studs higher relative to altplayer2
-- That means: altplayer2.CFrame = altplayer1's torso + 4 studs upwards relative to altplayer2's orientation
-- Since altplayer2 has its own orientation, we offset along its up vector by 4 studs
local alt1Pos = alt1HRP.Position
local alt2UpVector = alt2Torso.CFrame.UpVector -- Vector3 pointing upwards relative to altplayer2 torso
-- The final position for altplayer2 to match is alt1HRP's position + altplayer2's up direction * 4
local alt2TargetPos = alt1Pos + alt2UpVector * 4
-- Keep altplayer2's current rotation and just adjust position to this
alt2Torso.CFrame = CFrame.new(alt2TargetPos) * (alt2Torso.CFrame - alt2Torso.CFrame.Position)
-- Step 3: altplayer3 should do the same, but 5 studs down
-- So altplayer3 matches altplayer1's torso, but 5 studs downward relative to altplayer3's orientation
local alt3UpVector = alt3Torso.CFrame.UpVector
local alt3TargetPos = alt1Pos - alt3UpVector * 5
alt3Torso.CFrame = CFrame.new(alt3TargetPos) * (alt3Torso.CFrame - alt3Torso.CFrame.Position)
end)
Explanation:
We find all players based on name. You should replace specifiedPlayerName, altplayer1Name, etc., with actual player names.
We use HumanoidRootPart for positioning. If not available, fall back to Torso or UpperTorso.
For altplayer1, we:
Start with specifiedPlayer's CFrame.
Rotate 90 degrees downward around the X-axis.
Move 5 studs down (world Y axis).
For altplayer2 and altplayer3:
We move them relative to their own local upward direction (up vector from CFrame).
altplayer2 is moved 4 studs up relative to its own orientation.
altplayer3 is moved 5 studs down relative to its own orientation.
Notes
This code runs every frame updating positions.
If you want a more permanent weld or attachment, use Motor6Ds or WeldConstraints instead.
Depending on the actual rig (R6 vs R15), part names might differ - adjust getTorso accordingly.
Adjust player references and part names as needed.
If you want me to tailor this script specifically for your environment or clarify any steps, let me know!