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

Part of Player need to LookAt Mouse position ?

Hey, My Character is a Tank. Right now my tank is moving with W,A,S,D.

https://i.imgur.com/VeZzInb.gif

I need the canon to always face the Mouse.

I saw that script here https://scriptinghelpers.org/questions/74839/character-wont-look-at-mouse-pointer And I tried to make it fit my needs, but it didnt work

local TankHead = game.StarterPlayer.StarterCharacter.CanonHead
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local rs = game:GetService("RunService").RenderStepped

rs:Connect(function()
	local hrp = TankHead:WaitForChild("Tank_Cannon_1")

	local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
	TankHead:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))

end)

https://i.imgur.com/cPQpMEh.png

Someone Have an idea ?

IS it because the Canon is Rig ?

1 answer

✓ Accepted answer

you're modifying the StarterCharacter instead of the player's actual character

StarterCharacter is just cloned when the player spawns, modifications to it aren't applied to the player's actual character until the player respawns

-- make sure this script is in StarterCharacterScripts or else it's not going to work when the player respawns
local player = game.Players.LocalPlayer
local TankHead = player.Character:WaitForChild("CanonHead")
local mouse = player:GetMouse()

local rs = game:GetService("RunService").RenderStepped

rs:Connect(function()
    local hrp = TankHead:WaitForChild("Tank_Cannon_1")

    local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
    TankHead:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))

end)

also, you might want to disable Humanoid.AutoRotate so the character doesn't rotate at all when you press w, a, s, or d

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