Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make the mouse lock on to a HumanoidRootPart?

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.

10 comments

0

Don't get the character from Workspace. get it from the Character property of Player.

0

you cant change the pos of a mouse atleast thats what i assume what your doing i didnt actually read the script

0

what do you mean, "get it from the character property of player"

0

Get the character from Player.Character, not from game.Workspace:WaitForChild(Player.Name)

0

mattchew yes i am trying to change the position of the mouse, so do i have to get the camera instead?

0

incapaz that has nothing to do with the script, and it works perfectly that way.

0

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.

0

i guess you're right

0

but that's besides the point rn

0

I Found that using "if Something == false then" doesn't work. So I did "if not Something then" and "if Something then" usually works.

3 answers

✓ Accepted answer

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.

1 comment

0

That doesn't work it expects something from line 18

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.

4 comments

0

it's already in 3rd person mode, but could u give me an example of code on what u just said?

0

You'd set Player.CameraMode = Enum.CameraMode.LockFirstPerson

0

i don't want first person, because i've already got 3rd person for the camera

0

What I am suggesting is you lock them to first person, point them, and then return them to 3rd person. There's not too many other ways to move the cursor.

i already have answered this question, but it's alright. u guys are the best for answering my question :D

This discussion is preserved as it was. New answers, comments, and votes are closed.