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.

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

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!

Same case, it still spinned the compass

It’s a bit sloppy but,

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 script.Parent.ImageLabel.Rotation >= 270 and goal.Rotation <= 90 then
		if tween then
			tween:Cancel()
		end
		script.Parent.ImageLabel.Rotation = 0
		tween = TweenService:Create(script.Parent.ImageLabel, tweenInfo, goal)
		tween:Play()
	elseif script.Parent.ImageLabel.Rotation <= 90 and goal.Rotation >= 270 then
		if tween then
			tween:Cancel()
		end
		script.Parent.ImageLabel.Rotation = 360
		tween = TweenService:Create(script.Parent.ImageLabel, tweenInfo, goal)
		tween:Play()
	else
		if tween then
			tween:Cancel()
		end
		tween = TweenService:Create(script.Parent.ImageLabel, tweenInfo, goal)
    	tween:Play()
	end
    script.Parent.TextLabel.Text = ang
end

This works but it snaps. I tried making it tween to 359.99 then setting the rotation to 0, then proceeding the tween but it seems to not work. Do what you can with the script :slight_smile: