I am trying to adjust a player’s HumanoidRootPart’s Vector3 position so that the NPC doesn’t get too close and push the player. I tried assigning the player’s position to a variable, but how do you adjust the Vector3 of a changing position (player is walking around)
My code (server script):
-- what I currently have:
while true do -- updates playerPos every 0.4 seconds
local playerPos = player.Character.HumanoidRootPart.Position --player's position
wait(0.4)
end
-- what I want to achieve: say player coords is (25, 50, 25)
while true do
local playerPos = Vector3.new(25, 50, 25)
playerPos.X = playerPos.X - 1
playerPos.Z = playerPos.Z - 1
print(playerPos) -- player position will be offset by one in X and Z direction
wait(0.4)
end
--Output:
24, 50, 24
tl;dr: I just don’t know how to convert HumanoidRootPart.Position
to a Vector3 that I can manipulate.
You would have to write code like this:
local rootPos = player.Character.HumanoidRootPart.Position
local newPos = Vector3.new(rootPos.X, rootPos.Y, rootPos.Z)
Then you can adjust the Vector3 by adding numbers to it
2 Likes
You should put your while true
loops at the bottom of the script since it blocks the execution of the script.
Also, you need to set the value to the property, since Humanoid.Position
returns a value, not a pointer to the value.
Another mistake I see you making is you’re overwriting the playerPos
with set coordinates, which would either make the player jitter between two places or stay in one place.
1 Like
The reason that it isn’t working is because you’re adjusting the value you already got, which doesn’t actually update the character’s position. You need to use this function:
player.Character:SetPrimaryPartCFrame(CFrame.new(playerPos))
after you’re done adjusting the playerPos
variable. (it goes directly above the print statement)