So I'm developing for a DB game, (Dragon Ball) and I wanted to make a locking mouse system where your mouse locks onto a HumanoidRootPart. But instead, it doesn't lock on, and does nothing but give and error; "p cannot be assigned". Here's the script:
local Player = game.Players.LocalPlayer local Character = game.Workspace:WaitForChild(Player.Name) 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 while LockedOn == true do Mouse.Hit.p = Mouse.Target.Parent:WaitForChild("HumanoidRootPart").CFrame wait(0.1) end end elseif LockedOn == true then LockedOn = false end end end)
Please help me as I want to get this done soon.
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.
You cannot control the position of the mouse directly.
That being said, a quick and dirty way of doing this would be to lock the player's camera to first person, and then point the camera at the part. You can then unlock the camera. This will be very jarring, but there's not much else that can be done.
i already have answered this question, but it's alright. u guys are the best for answering my question :D