Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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.

0
Don't get the character from Workspace. get it from the Character property of Player. User#19524 175 — 6y
0
you cant change the pos of a mouse atleast thats what i assume what your doing i didnt actually read the script mattchew1010 396 — 6y
0
what do you mean, "get it from the character property of player" PICKLECOM3000 2 — 6y
0
Get the character from Player.Character, not from game.Workspace:WaitForChild(Player.Name) User#19524 175 — 6y
View all comments (6 more)
0
mattchew yes i am trying to change the position of the mouse, so do i have to get the camera instead? PICKLECOM3000 2 — 6y
0
incapaz that has nothing to do with the script, and it works perfectly that way. PICKLECOM3000 2 — 6y
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. User#19524 175 — 6y
0
i guess you're right PICKLECOM3000 2 — 6y
0
but that's besides the point rn PICKLECOM3000 2 — 6y
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. TrinityDevelepment 0 — 3y

3 answers

Log in to vote
0
Answered by
oilsauce 196
6 years ago

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.

0
That doesn't work it expects something from line 18 DybalaplaysYT 0 — 4y
Ad
Log in to vote
0
Answered by
Faeyll 22
6 years ago

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.

0
it's already in 3rd person mode, but could u give me an example of code on what u just said? PICKLECOM3000 2 — 6y
0
You'd set Player.CameraMode = Enum.CameraMode.LockFirstPerson User#19524 175 — 6y
0
i don't want first person, because i've already got 3rd person for the camera PICKLECOM3000 2 — 6y
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. Faeyll 22 — 6y
Log in to vote
0
Answered by 6 years ago

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

Answer this question