Local Script in StarterGui
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Move:Connect(function()
if player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil then
local root = player.Character.HumanoidRootPart
player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (mouse.Hit.p * Vector3.new(1,0,1)) + (root.Position * Vector3.new(0, 1, 0))))
end
end)
The function simply checks to see if the mouse is moving and if the player's character is loaded.
SetPrimaryPartCFrame moves the entire player (since the character is a model)
The above example would be a continuous variant, but the main part to see that the player faces the mouse is this:
local root = player.Character.HumanoidRootPart
player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (mouse.Hit.p * Vector3.new(1,0,1)) + (root.Position * Vector3.new(0, 1, 0))))
COMMENT EDIT
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local humanoid = player.Character:WaitForChild("Humanoid")
humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
mouse.Move:Connect(function()
if script.Prevent.Value == false and player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil then
local root = player.Character.HumanoidRootPart
player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (mouse.Hit.p * Vector3.new(1,0,1)) + (root.Position * Vector3.new(0, 1, 0))))
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.Up or inputObject.KeyCode == Enum.KeyCode.Down or inputObject.KeyCode == Enum.KeyCode.Left or inputObject.KeyCode == Enum.KeyCode.Right or inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.D or inputObject.KeyCode == Enum.KeyCode.Thumbstick1 or inputObject.KeyCode == Enum.KeyCode.DPadDown or inputObject.KeyCode == Enum.KeyCode.DPadRight or inputObject.KeyCode == Enum.KeyCode.DPadLeft or inputObject.KeyCode == Enum.KeyCode.DPadUp then
script.Prevent.Value = true
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.Up or inputObject.KeyCode == Enum.KeyCode.Down or inputObject.KeyCode == Enum.KeyCode.Left or inputObject.KeyCode == Enum.KeyCode.Right or inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.D or inputObject.KeyCode == Enum.KeyCode.Thumbstick1 or inputObject.KeyCode == Enum.KeyCode.DPadDown or inputObject.KeyCode == Enum.KeyCode.DPadRight or inputObject.KeyCode == Enum.KeyCode.DPadLeft or inputObject.KeyCode == Enum.KeyCode.DPadUp then
script.Prevent.Value = false
end
end)
Since you are using the Continuous method, this will circumvent the glitching out for that use.
For this example, i added a BoolValue under the LocalScript named "Prevent" (initially set to false)
Whenever the player begins to move (by pressing down any of the control buttons listed above) the value will become false and prevent the mouse function from running, otherwise, when in stand-still, the player will face the mouse.
1 comment
Classic