im trying to make an attack where you do a wind up then teleport to the nearest player or rig in front of you then do a attack on them, i could just move the player to the opponents position -2 but idk if that would work. how would I do this?
Pivoting to a target character’s CFrame and offsetting it by -2 studs would work, just do something like this
local character = script.Parent
local function teleport(targetCharacter : Model) : ()
character:PivotTo(targetCharacter * CFrame.new(0, 0, -2)
end
It’ll put you 2 studs in front of the target character
Hey! I made this script which I think should work. It should be a server script and you should have a variable “plr” that contains the player teleporting. I first get the nearest player by looping through all of them, and then teleport it. Let me know if you have any doubts!
local player_char = plr.Character or plr.CharacterAdded:Wait()
local player_hrp = player_char:WaitForChild("HumanoidRootPart")
local nearest_distance
local nearest_position
for i,v in pairs(game.Players:GetPlayers()) do
local char = v.Character
if not char then continue end
local hrp = char:FindFirstChild("HumanoidRootPart")
if not hrp then continue end
if (player_hrp.Position - hrp.Position).Magnitude <= nearest_distance then
nearest_distance = (player_hrp.Position - hrp.Position).Magnitude
nearest_position = hrp.Position
end)
end
if nearest_position then
player_char:PivotTo(CFrame.new(nearest_position))
end
this puts you in front of the opponent yes, but it faces you away from them. i think i should rephrase what i want, i want the player to move forward so that it closes the distance to the opponent so that you move to them so that if you’re behind them it puts you behind them and if you’re in front of them you are in front and the sides etc.
local character = script.Parent
local function teleport(targetCharacter : Model) : ()
character:PivotTo(targetCharacter * CFrame.new(0, 0, -2) * CFrame.Angles(0, math.pi, 0)
end
i don’t wanna use pivot to because that won’t work how i want it, I want it to check the distance between the 2 players and move the player that distance.
before the attack
after the attack is used
i want it to just move me forward to the dummy dependent on distance and not just teleporting in front of them
This should do what you want it to:
local function teleport(targetCharacter : Model) : ()
local characterPosition = character:GetPivot().Position
local targetPosition = targetCharacter:GetPivot().Position
local toTargetFlattened = (targetPosition * Vector3.new(1, 0, 1) - characterPosition * Vector3.new(1, 0, 1)).Unit
local newCFrame = CFrame.lookAlong(targetPosition, toTargetFlattened) * CFrame.new(0, 0, 3)
character:PivotTo(newCFrame)
end
This uses flattened character positions so they don’t angle weirdly when there’s a height difference
this kinda works but its a little weird
it flips the characters around
What do you mean by flipped around


