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

How to get a brick to move towards the mouse target?

I was programming a Magic system for my game, when I realized; I have literally no clue how to get a brick to actually move towards the mouse. I understand mouse Targets, hits, and the like, but I've tried methods like :MoveTo() and BodyVelocity, but neither of them worked very well.

(This section is for people who don't understand what I'm asking) All in all, I need a brick to spawn ontop of the Player Character's UpperTorso, and then move to the mouse's Target.

If any of you could give me a point in the right direction, that would be great!

EDIT: Also, here's the code I'm using, if that helps in any way:

mouse.KeyDown:connect(function(key)
	if (key == 'q') then 
		-- This script freezes the player midair, and has them turn to face the mouse.
		RotateRootPart = true
		local RootPart = character.HumanoidRootPart
		RootPart.Anchored = true
		local mouse = player:GetMouse()

		mouse.Move:connect(function() 
			if (RotateRootPart == true) then
   			RootPart.CFrame = CFrame.new(RootPart.Position, mouse.Hit.p)
			elseif (RotateRootPart == false) then
			end
		end)
		wait(1.5)
		local NewGroup = Instance.new('Model', game.Workspace)	
		local BlindingFog = Instance.new('Part', NewGroup)
		BlindingFog.TopSurface = 'Smooth'
		BlindingFog.BottomSurface = 'Smooth'
		BlindingFog.CanCollide = false	
		BlindingFog.Position = character.UpperTorso.Position
		wait(1.5)
		RotateRootPart = false -- Disables the effect of turning to face the mouse.
		RootPart.Anchored = false -- Drops the player.
	end
end)

2 comments

0

KeyDown is deprecated. Don't use it.

0

Oof. KK.

1 answer

-- Local Script
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local inputService = game:GetService("UserInputService")
local char = plr.CharacterAdded:Wait()

inputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        RotateRP = true
        RP = char.HumanoidRootPart
        mouse.Move:Wait() -- Do this. It's like doing :Connect(function() but more efficient in here.
        if RotateRP then
            RP.CFrame = CFrame.new(RootPart.Position, mouse.Hit)
        elseif not RotateRP then
            -- Nothing
        end

        wait(1.5)

        local model = Instance.new("Model",game.Workspace)
        local part = Instance.new("Part",model)
        part.CFrame = char.UpperTorso.CFrame + Vector3.new(2,0,0) -- Just so it doesn't glitch in char
        part.TopSurface = Enum.SurfaceType.Smooth -- Use this not "Smooth"
        part.BottomSurface = Enum.SurfaceType.Smooth
        part.CanCollide = false
        RotateRP = false
        RP.Anchored = false
        while wait() do
            part.CFrame = mouse.Hit
        end
    end
end)

wait(1.5)


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