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: