First of all, Mouse.Hit.p is a Vector3 value, and not a CFrame value. Mouse.Hit is already a CFrame value, so if you're trying to set the Mouse's CFrame, you don't need the .p at the end.
Mouse.Hit = Mouse.Target.Parent:WaitForChild("HumanoidRootPart").CFrame
However, you cannot change the Mouse's Position, as they're a readonly property.
You can change Mouse.Target.Parent:WaitForChild("HumanoidRootPart").CFrame to the Mouse's CFrame (Mouse.Hit) like this:
Mouse.Target.Parent:WaitForChild("HumanoidRootPart").CFrame = Mouse.Hit
but like I said, you cannot set the Mouse's position to another position via a script.
Also, it is more safe and effient to get your own Character from the Player (Player.Character) , rather than getting it from the Workspace (game.Workspace:WaitForChild(Player.Name)).
If you want to Lock On to a Character's HumanoidRootPart, what I would do is make my own HumanoidRootPart to look at the opponent's HumanoidRootPart. Like this:
local Player = game.Players.LocalPlayer
local Character = Player.Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local UIS = game:GetService("UserInputService")
local LockedOn = false
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if gameProcessedEvent then return end
if Input.KeyCode == Enum.KeyCode.E then
if LockedOn == false then
LockedOn = true
if Mouse.Target.Parent:WaitForChild("Humanoid") then
local opponentsRootPart = Mouse.Target.Parent:WaitForChild("HumanoidRootPart")
while LockedOn == true do
HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.p, Vector3.new(opponentsRootPart.CFrame.x, HumanoidRootPart.CFrame.y, opponentsRootPart.CFrame.z)
wait(0.1)
end
end
elseif LockedOn == true then
LockedOn = false
end
end
end)
One more thing, using a while loop multiple times is not ideal.
In this case, you should use a repeat loop, as it only loops until a certain condition.
From this:
while LockedOn == true do
HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.p, Vector3.new(opponentsRootPart.CFrame.x, HumanoidRootPart.CFrame.y, opponentsRootPart.CFrame.z)
wait(0.1)
end
to this:
repeat wait(0.1)
HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.p, Vector3.new(opponentsRootPart.CFrame.x, HumanoidRootPart.CFrame.y, opponentsRootPart.CFrame.z)
until LockOn == false
This will repeat its loop every 0.1 second until the LockOn value is false.
I hope I helped in any way, and I hope you find your best solution.
10 comments
Don't get the character from Workspace. get it from the Character property of Player.
you cant change the pos of a mouse atleast thats what i assume what your doing i didnt actually read the script
what do you mean, "get it from the character property of player"
Get the character from Player.Character, not from game.Workspace:WaitForChild(Player.Name)
mattchew yes i am trying to change the position of the mouse, so do i have to get the camera instead?
incapaz that has nothing to do with the script, and it works perfectly that way.
Just because it works doesn't mean it should be used. That script would break if a user named Terrain joined the game lol. And you could put anything with the same name of the player, and the Character variable you have would refer to that, not the character.
i guess you're right
but that's besides the point rn
I Found that using "if Something == false then" doesn't work. So I did "if not Something then" and "if Something then" usually works.