The player wont face where the player clicked

For some reason, the player character doesn’t rotate the whole way to where the mouse click.

The dot indicates precisely where I tapped.

local script

        local player = game.Players.LocalPlayer

        script.Parent.MouseButton1Down:Connect(function()

        	local Mouse = player:GetMouse()
        	
        	local MouseFunction

        	MouseFunction = Mouse.Button1Down:Connect(function()
        		
        	    player.Character.PrimaryPart.CFrame = CFrame.new(player.Character.PrimaryPart.Position,Vector3.new(Mouse.hit.Position.X,player.Character.PrimaryPart.Position.Y,Mouse.hit.Position.Z))

        		MouseFunction:Disconnect()	
        	end)
        end)

I’ve tried doing the rotation code in a script but the same thing happen.

edit: if you’re wondering why there’s script.Parent.MouseButton1Down. This is a code that is inside that Ultimatekick Gui. Basically you have to tap/click on it and then tap/click anywhere on the screen to fire it. When you tap/click again, it will get where you click/tap, I made it so the character face that way, but it won’t work.

1 Like
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()


mouse.Button1Down:Connect(function()
    
   char.HumanoidRootPart.CFrame = CFrame.lookAt(char.HumanoidRootPart.Position, mouse.Hit.Position)

end)
1 Like

sorry, it doesn’t work, the same thing happened, it doesn’t like rotate the whole way like just before.

It works just fine for me. I face the position where I clicked with my mouse.

1 Like

Hang on, lemme try to do it with a click only, without having to click the gui first and then clicking. The thing is, I did it with another thing on touchscreen, it work perfectly fine, the pc version of this also work fine but I just don’t know why this one won’t work.

So pc works but not on mobile?

1 Like

ye but also another gui works perfectly too, that is on mobile but not this one when it pretty much has the same code, to rotate.

I think, I might found the issue, lemme try to see if it is.

try maybe using the mouse’s LookVector?
ex:

local rootPart = player.Character.PrimaryPart

rootPart.CFrame = CFrame.new(rootPart.CFrame.Position, rootPart.CFrame.Position + Vector3.new(mouse.Hit.LookVector.X, 0, mouse.Hit.LookVector.Z))

Using CFrame.new to make something face something is deprecated. And his problem is that it won’t work on mobile.

aight, so I found out that after testing, no matter where your cam face and no mater where you click it faces slightly to the right, you can probs notice that in the vid(happened all the time when I tested)

also I’ve done that before, same thing happened.

You shouldn’t use the mouse object as it has been superseded by UserInputService and likely will be deprecated soon. You could use workspace.CurrentCamera:ScreenPointToRay() with the player’s 2D tap/mouse position. Then, you could use a raycast using the unit ray it returns and make the player face the raycast’s hit position.

oh ok, Can u show me what the code look likes because I haven’t really used raycast and those type of stuff before, thanks.

(for pc, you could use something like TouchTapInWorld for mobile)
ex:

--add other variables like "character", etc.
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, processed)
    if processed then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local location = UserInputService:GetMouseLocation()
        local unitRay = workspace.CurrentCamera:ScreenPointToRay(location.X, location.Y)

        local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 10000) -- you could add raycast params if you want
        character.PrimaryPart.CFrame = CFrame.lookAt(character.PrimaryPart.Position, raycastResult.Position)
    end
end)

edit: fixed something idk if it changes anything though.

1 Like

Thanks for the reply, Is the TouchTapInWorld meant to be in the location variable
local location = UserInputService:TouchTapInWorld()

TouchTapInWorld is a UserInputService event. The location is already given to you, no need to create your own one.

ex:

UserInputService.TouchTapInWorld:Connect(function(location, processed)

end)
2 Likes

Thanks you a lot, it works now

2 Likes