Rotating GUI bar to face mouse

Hello! I’m trying to make a bar face where the mouse is pointing. Here’s a gif what I’ve got so far,

https://gyazo.com/8fb981585e174f86817a3db743fde9c3

As you can see there’s a small rotation for the bar but it’s not working entirely. It only rotates a small bit instead of entirely rotating to face the mouse. The code is below, currently, I’m using math.atan2.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

game:GetService("RunService").Stepped:Connect(function()
    local x = math.atan2(mouse.X,mouse.Y)
    playergui:WaitForChild("testGui").Frame.Rotation = x
end)

30%20PM

5 Likes


https://developer.roblox.com/articles/Lua-Libraries/math

1 Like

Whoops. I mixed up X and Y. Either way though, same issue.

1 Like

I assume this is what you wanted?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local playergui = game.Players.LocalPlayer.PlayerGui

game:GetService("RunService").Stepped:Connect(function()
	local frame = playergui:WaitForChild("testGui").Frame
	local frameCenter = frame.AbsolutePosition + (frame.AbsoluteSize/2)
    local x = math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X)
    frame.Rotation = math.deg(x)
end)
2 Likes

It’s almost perfect! Just instead of the side facing the mouse the top would be

1 Like
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local playergui = game.Players.LocalPlayer.PlayerGui

game:GetService("RunService").Stepped:Connect(function()
	local frame = playergui:WaitForChild("testGui").Frame
	local frameCenter = frame.AbsolutePosition + (frame.AbsoluteSize/2)
    local x = math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X)
    frame.Rotation = math.deg(x) + 90
end)
5 Likes

I think you can just do

frame.Rotation = math.deg(x) + 90
4 Likes

You could just add 90 degrees to the final rotation and it would be fine I think.

1 Like

We had the same idea. Thanks a million!

1 Like