can you make a lua script that will make altplayer1 match position and rotation of…
- User
can you make a lua script that will make altplayer1 match position and rotation of specifiedPlayer when being rotated by 90 degrees downwards and 2 studs down, and specifiedPlayer hipheight is going to be set to 2
- Assistant
Certainly! Here's a Lua script that assumes you're working in Roblox. This script aligns
altplayer1tospecifiedPlayer’s position and rotation but rotated 90 degrees downwards and moved 2 studs down. It also setsspecifiedPlayer’s HipHeight to 2.local Players = game:GetService("Players") -- Replace these with your actual player references local altplayer1 = Players:FindFirstChild("altplayer1") local specifiedPlayer = Players:FindFirstChild("specifiedPlayer") if altplayer1 and specifiedPlayer then local altChar = altplayer1.Character local specifiedChar = specifiedPlayer.Character if altChar and specifiedChar then local altHumanoidRootPart = altChar:FindFirstChild("HumanoidRootPart") local specifiedHumanoidRootPart = specifiedChar:FindFirstChild("HumanoidRootPart") local specifiedHumanoid = specifiedChar:FindFirstChildOfClass("Humanoid") if altHumanoidRootPart and specifiedHumanoidRootPart and specifiedHumanoid then -- Set specifiedPlayer HipHeight to 2 specifiedHumanoid.HipHeight = 2 -- Get specifiedPlayer's current position and orientation local origCFrame = specifiedHumanoidRootPart.CFrame -- Define a rotation of 90 degrees downwards around the X axis. -- 90 degrees down means rotating forward/down pitch by 90 degrees. -- RotationMatrix: CFrame.Angles(math.rad(90), 0, 0) -- Offset 2 studs down (in world Y axis) local offset = Vector3.new(0, -2, 0) -- Calculate new CFrame for altplayer1 -- Rotate specifiedPlayer orientation by 90 degrees downwards local rotatedCFrame = origCFrame * CFrame.Angles(math.rad(90), 0, 0) -- Apply offset (2 studs down) rotatedCFrame = rotatedCFrame + offset -- Set altplayer1's HumanoidRootPart to this new CFrame altHumanoidRootPart.CFrame = rotatedCFrame else warn("Missing HumanoidRootPart or Humanoid for players.") end else warn("Missing character for players.") end else warn("Players not found in game.") endExplanation:
- The script finds the two players by name.
- Gets their characters and essential parts.
- Sets the
specifiedPlayer's HipHeight to 2. - Gets the specifiedPlayer's root part CFrame.
- Applies a 90-degree rotation downward (pitch) by rotating around the X axis by 90 degrees.
- Offsets by 2 studs down in world Y (vertical).
- Sets
altplayer1's HumanoidRootPart CFrame to match this transformed CFrame.
Let me know if you want me to tailor this script for another environment or add more functionality.
- User
also can u make altplayer2 match position and rotation of altplayer1 when being 3 studs more up relative to altplayer1 and also altplayer2 will be rotated by 30 degrees while being 1 stud up relative to workspace
- Assistant
Certainly! Below is an updated script that does both:
- Makes
altplayer1match the position and rotation ofspecifiedPlayerrotated 90 degrees downward and offset 2 studs down; setsspecifiedPlayer's HipHeight to 2. - Makes
altplayer2match the position and rotation ofaltplayer1, offset by 3 studs up relative toaltplayer1. - Rotates
altplayer2by an additional 30 degrees (around Y axis assumed since not specified; you can adjust if needed). - Additionally, moves
altplayer21 stud up relative toworkspace(world space Y+).
local Players = game:GetService("Players") local altplayer1 = Players:FindFirstChild("altplayer1") local altplayer2 = Players:FindFirstChild("altplayer2") local specifiedPlayer = Players:FindFirstChild("specifiedPlayer") if altplayer1 and altplayer2 and specifiedPlayer then local altChar1 = altplayer1.Character local altChar2 = altplayer2.Character local specifiedChar = specifiedPlayer.Character if altChar1 and altChar2 and specifiedChar then local altHRP1 = altChar1:FindFirstChild("HumanoidRootPart") local altHRP2 = altChar2:FindFirstChild("HumanoidRootPart") local specifiedHRP = specifiedChar:FindFirstChild("HumanoidRootPart") local specifiedHumanoid = specifiedChar:FindFirstChildOfClass("Humanoid") if altHRP1 and altHRP2 and specifiedHRP and specifiedHumanoid then -- Part 1: Set specifiedPlayer HipHeight specifiedHumanoid.HipHeight = 2 -- Part 2: Position and rotate altplayer1 relative to specifiedPlayer local baseCFrame = specifiedHRP.CFrame local downwardRotation = baseCFrame * CFrame.Angles(math.rad(90), 0, 0) local alt1CFrame = downwardRotation + Vector3.new(0, -2, 0) altHRP1.CFrame = alt1CFrame -- Part 3: Position and rotate altplayer2 relative to altplayer1 -- Offset altplayer2 3 studs up relative to altplayer1 local upOffset = Vector3.new(0, 3, 0) local alt2BasePos = altHRP1.Position + upOffset -- Rotate altplayer2 by 30 degrees (assuming Y axis rotation) local rotation30Degrees = CFrame.Angles(0, math.rad(30), 0) -- We want altplayer2 facing altplayer1's orientation, rotated by 30 degrees -- Start from altplayer1's rotation local alt1Rotation = CFrame.new(Vector3.new(), altHRP1.CFrame.LookVector) -- Combine with 30 deg rotation local alt2Rotation = altHRP1.CFrame * rotation30Degrees -- This keeps altplayer1's position, rotated -- We want altplayer2 at alt2BasePos, so set its CFrame keeping rotation at alt2Rotation's rotation but position alt2BasePos local alt2CFrame = CFrame.new(alt2BasePos) * (alt2Rotation - alt2Rotation.p) -- Finally, add 1 stud up relative to workspace (world space) alt2CFrame = alt2CFrame + Vector3.new(0, 1, 0) altHRP2.CFrame = alt2CFrame else warn("Missing required HumanoidRootPart or Humanoid in characters.") end else warn("Missing characters for players.") end else warn("One or more players not found in the game.") end
Explanation:
specifiedPlayer'sHipHeightset to2.altplayer1positioned relative tospecifiedPlayer:- Rotated 90 degrees downward (around X axis).
- Offset 2 studs down in world space.
altplayer2positioned relative toaltplayer1:- 3 studs up relative to
altplayer1's position. - Rotated 30 degrees around Y axis relative to
altplayer1's orientation. - Then offset 1 stud up relative to the world (workspace Y+).
- 3 studs up relative to
If you want this to run continuously (like in a loop or heartbeat), or respond to an event, let me know and I can help with that as well!
- Makes
- User
also can u make position and rotation of altplayer1 and 2 update continously
- Assistant
Certainly! To continuously update the positions and rotations of
altplayer1andaltplayer2every frame, we should run the logic inside aRunService.RenderSteppedorRunService.Heartbeatevent. This way the script constantly tracksspecifiedPlayer, and moves/rotatesaltplayer1andaltplayer2accordingly.Here's a complete script that assumes you place it in a LocalScript (or a Script if it has permission to control others):
local Players = game:GetService("Players") local RunService = game:GetService("RunService") local altplayer1 = Players:FindFirstChild("altplayer1") local altplayer2 = Players:FindFirstChild("altplayer2") local specifiedPlayer = Players:FindFirstChild("specifiedPlayer") if not (altplayer1 and altplayer2 and specifiedPlayer) then warn("One or more players not found.") return end -- Cache characters and humanoids local function getCharAndParts(player) local char = player.Character if not char then return nil, nil, nil end local hrp = char:FindFirstChild("HumanoidRootPart") local humanoid = char:FindFirstChildOfClass("Humanoid") return char, hrp, humanoid end RunService.Heartbeat:Connect(function() local specifiedChar, specifiedHRP, specifiedHumanoid = getCharAndParts(specifiedPlayer) local altChar1, altHRP1, _ = getCharAndParts(altplayer1) local altChar2, altHRP2, _ = getCharAndParts(altplayer2) if specifiedHRP and specifiedHumanoid and altHRP1 and altHRP2 then -- Set specifiedPlayer HipHeight (keep setting continuously in case it changes) specifiedHumanoid.HipHeight = 2 -- Calculate altplayer1's CFrame local baseCFrame = specifiedHRP.CFrame local downwardRotation = baseCFrame * CFrame.Angles(math.rad(90), 0, 0) local alt1CFrame = downwardRotation + Vector3.new(0, -2, 0) altHRP1.CFrame = alt1CFrame -- Calculate altplayer2's CFrame relative to altplayer1 local upOffset = Vector3.new(0, 3, 0) local alt2BasePos = altHRP1.Position + upOffset local rotation30Degrees = CFrame.Angles(0, math.rad(30), 0) -- Rotate altplayer2 relative to altplayer1's rotation local alt2Rotation = altHRP1.CFrame * rotation30Degrees local alt2CFrame = CFrame.new(alt2BasePos) * (alt2Rotation - alt2Rotation.p) alt2CFrame = alt2CFrame + Vector3.new(0, 1, 0) altHRP2.CFrame = alt2CFrame end end)
How This Works:
- Every frame (via
RunService.Heartbeat), the script:- Gets current
HumanoidRootPartandHumanoidof the players. - Sets
specifiedPlayer's HipHeight to 2. - Calculates and sets
altplayer1's position/rotation to matchspecifiedPlayerrotated 90 degrees downward and offset 2 studs down. - Calculates and sets
altplayer2's position and rotation relative toaltplayer1with the specified offsets and rotations.
- Gets current
Notes:
- If characters die or respawn, this script handles getting the correct HRPs every frame.
- You can modify the update rate by using
RunService.RenderStepped(runs before rendering) orRunService.Stepped(runs before physics). - Ensure the script runs in a context with permissions to set other player's positions (e.g., a server Script).
- If you want, I can help refactor this into a ModuleScript or add remote event handling to trigger this on demand. Just ask!
Let me know if you need that or anything else!
- Every frame (via
- User
also can u make altplayer1 be 4 studs more down
- Assistant
Certainly! To move
altplayer14 studs further down (in addition to the current 2 studs downward offset), you just need to update the downward offset from-2studs to-6studs (because 2 + 4 = 6 studs down).Here's the modified continuous update script with this change applied:
local Players = game:GetService("Players") local RunService = game:GetService("RunService") local altplayer1 = Players:FindFirstChild("altplayer1") local altplayer2 = Players:FindFirstChild("altplayer2") local specifiedPlayer = Players:FindFirstChild("specifiedPlayer") if not (altplayer1 and altplayer2 and specifiedPlayer) then warn("One or more players not found.") return end local function getCharAndParts(player) local char = player.Character if not char then return nil, nil, nil end local hrp = char:FindFirstChild("HumanoidRootPart") local humanoid = char:FindFirstChildOfClass("Humanoid") return char, hrp, humanoid end RunService.Heartbeat:Connect(function() local specifiedChar, specifiedHRP, specifiedHumanoid = getCharAndParts(specifiedPlayer) local altChar1, altHRP1, _ = getCharAndParts(altplayer1) local altChar2, altHRP2, _ = getCharAndParts(altplayer2) if specifiedHRP and specifiedHumanoid and altHRP1 and altHRP2 then -- Set specifiedPlayer HipHeight specifiedHumanoid.HipHeight = 2 -- altplayer1 offset changed: 2 studs down + 4 studs more down = 6 studs down local baseCFrame = specifiedHRP.CFrame local downwardRotation = baseCFrame * CFrame.Angles(math.rad(90), 0, 0) local alt1CFrame = downwardRotation + Vector3.new(0, -6, 0) -- Changed offset here altHRP1.CFrame = alt1CFrame -- altplayer2 relative to altplayer1 as before local upOffset = Vector3.new(0, 3, 0) local alt2BasePos = altHRP1.Position + upOffset local rotation30Degrees = CFrame.Angles(0, math.rad(30), 0) local alt2Rotation = altHRP1.CFrame * rotation30Degrees local alt2CFrame = CFrame.new(alt2BasePos) * (alt2Rotation - alt2Rotation.p) alt2CFrame = alt2CFrame + Vector3.new(0, 1, 0) altHRP2.CFrame = alt2CFrame end end)
Summary of the change:
- On this line:
local alt1CFrame = downwardRotation + Vector3.new(0, -6, 0) -- was -2, now -6 to push altplayer1 4 more studs down - Everything else remains the same.
Let me know if you'd like it integrated differently or have other adjustments!
- On this line: