Trying to rotate body to face the mouse and getting "interesting" results

So i am trying to rotate the players body to face the mouse with this:
INSIDE “STARTERPLAYERSCRIPT” IN LOCAL SCRIPT

local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
	tor = character:WaitForChild("UpperTorso")
	hum = character:WaitForChild("Humanoid")
end
local cam = game.Workspace.CurrentCamera
local equipped = false
local tor = character.UpperTorso
local hum = character.Humanoid
player.CharacterAdded:Connect(function()
	character = player.Character
	tor = character:WaitForChild("UpperTorso")
	hum = character:WaitForChild("Humanoid")
end)
local Mouse = player:GetMouse()
local prev_mousehit

game:GetService("RunService").RenderStepped:Connect(function()
	if (cam.Focus.p-cam.CoordinateFrame.p).magnitude > 1 then
		hum.AutoRotate = false
		if Mouse.Hit.p ~= prev_mousehit then
			tor.CFrame = CFrame.new(tor.Position, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z))
			prev_mousehit = Mouse.Hit.p
		end
	else
		hum.AutoRotate = true
	end
end)

The result is funny but not the result you’d expect.

I would imagine the solution is unlocking whatever joint is locked but what do i know? :thinking:
Also in first person it flat out doesn’t work. :grinning:

Try rotating the players Motor6D joint instead of their body part.

example code:

local waist = player.Character.UpperTorso.Waist
waist.C0 = CFrame.new(waist.C0.p, camera.CFrame.lookVector)

your rotating the UpperTorso directly, and it is welded to other BodyParts with Motor6D joints
so this is making the entire model move along with the UpperTorso.
But rotating the Motor6D instead should solve your issue.

1 Like

sure thing
(30 character limit)

I might make a fool of myself here but did you mean like this?
character:WaitForChild("UpperTorso").Waist

whole code:

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
	tor = character:WaitForChild("UpperTorso").Waist
	hum = character:WaitForChild("Humanoid")
end
local cam = game.Workspace.CurrentCamera
local equipped = false
local tor = character.UpperTorso.Waist
local hum = character.Humanoid
player.CharacterAdded:Connect(function()
	character = player.Character
	tor = character:WaitForChild("UpperTorso").Waist
	hum = character:WaitForChild("Humanoid")
end)
local Mouse = player:GetMouse()
local prev_mousehit

game:GetService("RunService").RenderStepped:Connect(function()
	if (cam.Focus.p-cam.CoordinateFrame.p).magnitude > 1 then
		hum.AutoRotate = false
		if Mouse.Hit.p ~= prev_mousehit then
			tor.CFrame = CFrame.new(tor.Position, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z))
			prev_mousehit = Mouse.Hit.p
		end
	else
		hum.AutoRotate = true
	end
end)

Try this, you might have to do some fine tuning, and instead of using mouse hit you could use cameras LookVector, also you’ll probably want to mix this up with waist and head movement, but i’m not fully sure of your end goal here.

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

if not character or not character.Parent then
    character = player.CharacterAdded:wait()
	tor = character:WaitForChild("UpperTorso").Waist
	hum = character:WaitForChild("Humanoid")
end

local cam = game.Workspace.CurrentCamera
local equipped = false
local waist
local hum

player.CharacterAdded:Connect(function()
	character = player.Character
	waist = character:WaitForChild("UpperTorso"):WaitForChild("Waist")
	hum = character:WaitForChild("Humanoid")
end)

local Mouse = player:GetMouse()
local prev_mousehit

game:GetService("RunService").RenderStepped:Connect(function()
	if (cam.Focus.p-cam.CoordinateFrame.p).magnitude > 1 then
		hum.AutoRotate = false
		if Mouse.Hit.p ~= prev_mousehit then
			waist.C0 = CFrame.new(waist.C0.p, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z))
			prev_mousehit = Mouse.Hit.p
		end
	else
		hum.AutoRotate = true
	end
end)
1 Like

Players.gxmikvid.PlayerScripts.TorsoRotate:26: attempt to index upvalue ‘hum’ (a nil value)

throws errors

sorry i will try to tune it

the code is running before hum has a value.

basically your code is running before the Character is loaded, i would copy the code and then throw it into the CharacterAdded event.

ooooh i see

btw End goal: the head and body moves with the camera / mouse and it allows for more combat options for guns and swords and stuff. sou can slash up/down/left/right not just left/right

thx for the help btw :grinning:

oh nice usage and no problem bub. Hopefully all goes well for your system, sounds like a really cool idea.

1 Like

Marked as solution… i can work from here.
Might come back cuz i am an idiot. :laughing:
Thank you for your time.

Guess who’s back.
it is still nil and i can’t figure out why?

player = game.Players.LocalPlayer
function onCharacterAdded()
	local character = player.Character
	if not character or not character.Parent then
	    character = player.CharacterAdded:wait()
		tor = character:WaitForChild("UpperTorso").Waist
		hum = character:WaitForChild("Humanoid")
	end
	
	local cam = game.Workspace.CurrentCamera
	local equipped = false
	local waist
	local hum
	
	player.CharacterAdded:Connect(function()
		character = player.Character
		waist = character:WaitForChild("UpperTorso"):WaitForChild("Waist")
		hum = character:WaitForChild("Humanoid")
	end)
	
	local Mouse = player:GetMouse()
	local prev_mousehit
	
	game:GetService("RunService").RenderStepped:Connect(function()
		if (cam.Focus.p-cam.CoordinateFrame.p).magnitude > 1 then
			hum.AutoRotate = false
			if Mouse.Hit.p ~= prev_mousehit then
				waist.C0 = CFrame.new(waist.C0.p, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z))
				prev_mousehit = Mouse.Hit.p
			end
		else
			hum.AutoRotate = true
		end
	end)
end
player.CharacterAdded:Connect(onCharacterAdded)

fixed a thing but still the errors… the errors
@codyorr4

try it like this

local player = game.Players.LocalPlayer
local character
local waist
local hum

	
local cam = game.Workspace.CurrentCamera
local equipped = false
local Mouse = player:GetMouse()
local prev_mousehit

function onCharacterAdded()
	character = player.Character
	waist = character:WaitForChild("UpperTorso"):WaitForChild("Waist")
	hum = character:WaitForChild("Humanoid")
	
	game:GetService("RunService").RenderStepped:Connect(function()
		if (cam.Focus.p-cam.CoordinateFrame.p).magnitude > 1 then
			hum.AutoRotate = false
			if Mouse.Hit.p ~= prev_mousehit then
				waist.C0 = CFrame.new(waist.C0.p, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z))
				prev_mousehit = Mouse.Hit.p
			end
		else
			hum.AutoRotate = true
		end
	end)
end


player.CharacterAdded:Connect(onCharacterAdded)
1 Like

image

hehe

1 Like

@UltimateRaheem
waist.C0 = CFrame.new(waist.C0.p, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z))
fix it as shown:
waist.C0 = CFrame.new(waist.C0.p, Vector3.new(Mouse.Hit.p.x,Mouse.Hit.p.y,Mouse.Hit.p.z-90))

@codyorr4
but it works… although when the mouse is behind the character he breaks his spine and won’t turn anaud with his lower body… i can thy to fix it

yeah like i said it will need some fine tuning (and to be clamped) also its probably best to use camera LookVector rather then mouse.Hit, depending on what you need i suppose, but yeah haha that’s pretty funny looking.

True.
Well it’s been an honor working with you. :grinning:
You seem helpfull.

1 Like

you too! and yeah i try to help with certain things, take care!

1 Like