Script doesnt make the player face the mouse

  1. What do you want to achieve? A script that makes the player face the mouse

  2. What is the issue? the player faces the mouse when they key is first pressed but doest continue following it

  3. What solutions have you tried so far? none so far

here’s my script:

-- services and variables
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local character = player.Character

local debounce = false

local ShieldEvent = ReplicatedStorage.FloraRemotes:FindFirstChild("FloraShield")

local function toggle(value)
	character.Blast.Disabled = value
	character.Symphony.Disabled = value
end

local bg
local connection

local onAnim = Instance.new("Animation")
onAnim.AnimationId = "rbxassetid://8483958299"

local loadedAnim = character.Humanoid:LoadAnimation(onAnim)

-- code
UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C and debounce == false then
		debounce = true

		toggle(true)
		ShieldEvent:FireServer("Shield On")
		local AnimationTracks = character.Humanoid:GetPlayingAnimationTracks()
		for i, track in pairs (AnimationTracks) do
			track:Stop()
		end
		-- this is the part of the script that controls the rotation and etc
		bg = Instance.new("BodyGyro", character.HumanoidRootPart)
		bg.maxTorque = Vector3.new(math.huge, math.huge, math.huge);
		bg.P = 10^7;
		bg.D = 100
		connection = RunService.RenderStepped:Connect(function()
			RunService.RenderStepped:Wait()
			local direction = (player:GetMouse().Hit.p - character.HumanoidRootPart.Position) * Vector3.new(1,0,1)
			bg.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + direction)
		end)
		task.wait(2)
		debounce = false
	end
end)

UserInputService.InputEnded:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C then
		toggle(false)
		connection:Disconnect()
		game.Debris:AddItem(bg, .1)
		ShieldEvent:FireServer("Shield Off")
	end
end)

ReplicatedStorage.FloraRemotes.FloraShieldOn.OnClientEvent:Connect(function()
	loadedAnim:Play()
end)

ReplicatedStorage.FloraRemotes.FloraShieldOff.OnClientEvent:Connect(function()
	loadedAnim:Stop()
end)

Alright, I believe by the way you’ve structured the script: you want the character to face the mouse only WHILE the chosen button is held.

I scripted and tested the following code and it does just that:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")

local mouse = plr:GetMouse()

local key = Enum.KeyCode.C
local db = false

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == key and not db then
		db = true
		
		local bg = Instance.new("BodyGyro")
		bg.maxTorque = Vector3.new(math.huge, math.huge, math.huge);
		bg.P = 10^7;
		bg.D = 100
		bg.Parent = hrp 

		RS.Heartbeat:Connect(function() -- Heartbeat for optimization
			local origin = hrp .Position -- Create variable to reduce redundancies
			local direction = (mouse.Hit.p - origin) * Vector3.new(1,0,1)
			bg.CFrame = CFrame.new(origin, origin + direction)
			
			if not UIS:IsKeyDown(key) then -- checks if player has released key
				db = false
				bg:Destroy()
			end
		end)

	end
end)

If this is not what you were asking in the original post please let me know so I can restructure the code to achieve the desired behavior.
If you have any questions please ask! :slight_smile:

I also removed some redundancies and depreciated code from the original script.

3 Likes