Spinning compass

Hello, I do not really know how to explain this but does anyone know why the compass spins every time the angle is 360?

https://gyazo.com/bc8c4f16c35bf9b92c1458f4890feb67

here is the code:

local plr = script.Parent.Parent.Parent.Name

local TweenService = game:GetService("TweenService")

local ang = 0 

while wait() do
    local look = game.Workspace.Camera.CFrame.LookVector
    local angle = math.atan2(look.X, look.Z) 
    ang = -math.deg(angle) % 360
    local goal = {}
    goal.Rotation = ang
    local tweenInfo = TweenInfo.new(0.6)
    local tween = TweenService:Create(script.Parent.ImageLabel, tweenInfo, goal)
    tween:Play()
    script.Parent.TextLabel.Text =  ang
end
3 Likes

I think its because of the while loop i’m not really sure.

Simple add:
tween.Completed:wait()
Make sure it’s after you play it, as nothing below will run. I’d suggest putting it just above the end of the loop.

Right now, you don’t actually wait for the current tween to run before attempting to tween it again. This will make the loop wait until the tween completed before looping again.


thats because everytime the number goes more or equal than 360 it goes back to 0, and since you tween it, its visible.
fix :

local plr = script.Parent.Parent.Parent.Name

local TweenService = game:GetService("TweenService")

local ang = 0 

while wait() do
    local look = game.Workspace.Camera.CFrame.LookVector
    local angle = math.atan2(look.X, look.Z) 
    ang = -math.deg(angle)
    local goal = {}
    goal.Rotation = ang
    local tweenInfo = TweenInfo.new(0.6)
    local tween = TweenService:Create(script.Parent.ImageLabel, tweenInfo, goal)
    tween:Play()
    script.Parent.TextLabel.Text =  ang
end

edit: im dumb it doesnt work i didnt test it lol

This wouldn’t fix it? Also providing a fixed version of the code isn’t a great way for him to learn.

what do you mean he wont learn, i clearly explained the error (also i noticed i was wrong lol)

It didn’t really fix the issue,

You should explain the issue, a potential fix and leave them to implement it. You can provide an example but a completely fixed version of the code which he can copy paste isn’t allowed.

Try my fix, let me know if it doesn’t work! :grinning:

Nope, it didn’t really fix the issue.

Try removing the “% 360” and use my previous suggestion.

I did remove the ‘’% 360" and added a "tween.Completed:Wait() but it didn’t fix it

You tried with the %360 still too?

Yes I did, I tried with %360 and without %360 but none of them fixed the issue

Try extending the wait to .5 instead of nothing.

try putting

-(math.deg(angle) % 360)

Same case, it didn’t fix the issue

try this:

local plr = script.Parent.Parent.Parent.Name

local TweenService = game:GetService("TweenService")

local ang = 0 
local tween;

while wait() do
    local look = game.Workspace.Camera.CFrame.LookVector
    local angle = math.atan2(look.X, look.Z) 
    ang = -math.deg(angle) % 360 -- also try -(math.deg(angle) % 360)
    local goal = {}
    goal.Rotation = ang
    local tweenInfo = TweenInfo.new(0.6)
    if tween then
        tween:Cancel()
    end
    tween = TweenService:Create(script.Parent.ImageLabel, tweenInfo, goal)
    tween:Play()
    script.Parent.TextLabel.Text =  ang
end

It didn’t really fix the issue

Hi, I just talked to you in DM’s but i’m thirsty for that solution.

If I was making this I would do it a different way.

First I would create a value in the GUI called North, this would be a Vector3Value and u would put the value as ‘1, 0, 0’. Then I would create a value to store the degrees it’s currently set at this can be a NumberValue and set it to 0.

CODE

game:GetService("RunService").RenderStepped:Connect(function(Delta)
	local Character = game.Players.LocalPlayer.Character
	if ( not Character ) then
		return
	end
	
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	if ( not HumanoidRootPart ) then
		return
	end
	
	local OffsetX, OffsetY, OffsetZ = CFrame.new(Vector3.new(), script.Parent.North.Value):ToEulerAnglesXYZ()
	
	script.Parent.Degrees.Value = math.floor(-HumanoidRootPart.Orientation.Y + 180 - math.deg(OffsetY))
end)

In your other script you can tween the GUI to the degree value!