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

How to make Player Completely, Dead on follow player mouse?

--Player Follows Mouse
		
		BodyG = Instance.new("BodyGyro",Character.HumanoidRootPart)
		BodyG.D = 200
		BodyG.P = 5000
		BodyG.maxTorque = Vector3.new(0, math.huge, 0)
		game:GetService("RunService").RenderStepped:connect(function()
			BodyG.CFrame = CFrame.new(Root.Position, Mouse.Hit.p)
		end)

Wherever i aim, the player is always a little bit off, I wonder if i was doing it correctly..Also this is script makes the player follow the mouse for a short period of time before the bodygyro is deleted, I am also starting to get a hang of BodyGyro and etc.

1 answer

Try this!

-- @author Validark
-- @filtering Client-Side
-- This should go in StarterCharacterScripts

-- Localizations
local pi = math.pi
local atan2 = math.atan2
local newCFrame = CFrame.new
local newCFrameAngles = CFrame.Angles

-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- Data
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local function FollowMouse()
	if HumanoidRootPart then -- Don"t run once HumanoidRootPart is removed
		-- Rotate Character to Face mouse
        local HumanoidRootPartPos = HumanoidRootPart.Position
        local Dir = (Mouse.Hit.p - HumanoidRootPartPos).unit
        HumanoidRootPart.CFrame = newCFrameAngles(0, atan2(Dir.X, Dir.Z) + pi, 0) + HumanoidRootPartPos
    end
end

local FollowMouseConnection = RunService.RenderStepped:Connect(FollowMouse)

EDIT: In response to the follow-up question, yes you can run this for a short period of time and at some point have the player stop following the mouse. The FollowMouse function is Connected to the RenderStepped Event of RunService. So to make it stop, simply Disconnect it!

-- Whenever you want to break the connection, call the following method
FollowMouseConnection:Disconnect()

2 comments

0

Thanks for this!, but is there a way for it to run this script for a short period of time then make the player stop following the mouse?

0

Adjusted Answer.

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