So pretty much whenever a player says Human shield a random player is brought in front of them. My problem is that the player will always be out on the x axis and I want them to appear always in front of the player.
Here is my code
elseif string.lower(message) == "human shield" then
local players = Players:GetChildren()
local randomPlayer = players[math.random(1, #players)]
local rP = game.Workspace:FindFirstChild(randomPlayer.Name)
local hrp = rP:FindFirstChild("HumanoidRootPart")
hrp.Position = player.Character:FindFirstChild("HumanoidRootPart").Position + Vector3.new(1, 0, 0)
end
Pretty much I just want the hrp position adjusted so it’s in front of the player that it’s being teleported to and i’m unsure how to do this
What you’re trying to do is teleport the player in front of your character, which would entail converting object space to world space. In object space, position is relative to the object/player (for example, if I were to look to my left, it would be -1, 0, 0) allowing for the transferring of the–relative–position to a global, world space. You would achieve this through CFrame:ToWorldSpace(). Writing it out, your code would look something like this:
elseif string.lower(message) == "human shield" then
local players = Players:GetChildren()
local randomPlayer = players[math.random(1, #players)]
local rP = game.Workspace:FindFirstChild(randomPlayer.Name)
local hrp = rP:FindFirstChild("HumanoidRootPart")
hrp.CFrame = player.Character:FindFirstChild("HumanoidRootPart").CFrame:ToWorldSpace(CFrame.new(0, 0, -1))
end
Not only would it maintain a position in front of your player by one stud, but it would also maintain it’s orientation due to how Coordinate Frames work.