I made a simple script using Collection Service for when the player triggers a proximity prompt they are supposed to walk in front of the part and look at it.
The problem here is that the player walks in front of the wrong direction of the part and always looks at the first part that they triggered the prompt from, also leaning.
And I didn’t notice before but when I tried to debug it, the prints duplicate every time the prompt is triggered! (Don’t know if that is supposed to happen)
Here is a video showing my problemo:
The green hexagon on the parts is indicating the front of the part, which I want the player to walk in front of.
Here is the code:
----------[Services]
local collectionService = game:GetService("CollectionService")
----------[Tags]
local elecBoxesPrompts = collectionService:GetTagged("ElecBox")
----[Electric Box]
for _,prompt in pairs(ElecBoxesPrompts) do
prompt.Triggered:Connect(function(player)
local electricalBox = prompt.Parent
--Position accordingly
local moveToPos = electricalBox.CFrame * Vector3.new(-3, 0, 0)
player.Character.Humanoid:MoveTo(moveToPos)
player.Character.Humanoid.MoveToFinished:Connect(function()
--Look at box
player.Character.HumanoidRootPart.Anchored = true
local charCF = player.Character:GetPrimaryPartCFrame()
local newCF = CFrame.new(charCF.Position, electricalBox.Position)
player.Character:SetPrimaryPartCFrame(newCF)
print(electricalBox.Name)
--Release
task.wait(2.5)
player.Character.HumanoidRootPart.Anchored = false
end)
print("--------")
end)
end
Because I know the leaning is from making the humanoid root part look at the electical box directly, which would tilt the character to look at the box exactly, so i’m thinking not anchoring the root part would make the character correct itself, or alternatively you could make newCF ignore y axis
I disabled anchoring the root part and that fixed the leaning, Thanks!
Still going to need help on the player looking at the wrong box and walking direction though. :[
Wait I see the problem! in move to pos you are multiplying electricalBox.CFrame by Vector3.new(-3, 0, 0), which would cause it to walk to the side of the box instead of in front so it should be
electricalBox.CFrame * Vector3.new(0, 0, 3) or electricalBox.CFrame * Vector3.new(0, 0, -3)
That fixed the positioning! Thank you!
I also did some tinkering and I found out how to fix the player looking at the wrong box. While also cleaning some stuff.
All I had to do was move some code around…
Thank you again for helping :]
The finalized code:
----------[Services]
local collectionService = game:GetService("CollectionService")
----------[Tags]
local elecBoxesPrompts = collectionService:GetTagged("ElecBox")
----[Electric box]
for _,prompt in pairs(ElecBoxesPrompts) do
prompt.Triggered:Connect(function(player)
local electricalBox = prompt.Parent
local playerRoot = player.Character.HumanoidRootPart
local playerHum = player.Character.Humanoid
--Position accordingly
local moveToPos = electricalBox.CFrame * Vector3.new(0, 0, -3)
playerHum:MoveTo(moveToPos)
--Lock Player
playerHum.MoveToFinished:Connect(function()
playerHum.WalkSpeed = 0
playerHum.JumpHeight = 0
end)
--Look at box
playerHum.MoveToFinished:Wait()
local charCF = player.Character:GetPrimaryPartCFrame()
local newCF = CFrame.new(charCF.Position, electricalBox.Position)
player.Character:SetPrimaryPartCFrame(newCF)
--Release Player
task.wait(2.5)
player.Character.Humanoid.WalkSpeed = 16
player.Character.Humanoid.JumpHeight = 7.2
end)
end