Radial menu mouse vector returns 0,0

I was sent this by a friend of mine and was asked if I could fix it up, but I am lost, myself.

  1. What do you want to achieve?
    Make it where the mouse position reutrns proper vector position, and where the cursor rotates around in a full circle to where you would be able to select the other buttons and not only the bottom right quadrant.

  2. What is the issue?
    The mouse position (mouselocation2) returns as 0,0 which does not rotate around. Also, changing the math from subtraction to addition, it allows you to rotate, but it stays around in the bottom right quadrant, and also does not allow you to select the close button which forces you to use the open/close button, or select a button to close.

  3. What solutions have you tried so far?
    I have tried looking around for similar scripts that do the same thing to gain some insight on how it should properly be, though I’ve ended up empty handed, and as a result I am here.

I have tried changing line 78 from

mouseLocation2 = mouseLocation2 + (userinputService:GetMouseLocation() - mouseLocation1) / 2

to

mouseLocation2 = mouseLocation2 + (userinputService:GetMouseLocation() + mouseLocation1) / 2

However, this doesn’t do what I’d like it to do as it only stays in the bottom right.


radialmenu.rbxl (62.4 KB)

local player = game:GetService("Players").LocalPlayer
local userinputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local character = player.Character or player.CharacterAdded:Wait()

local assets = player.Character:FindFirstChild("Assets") or player.Character:WaitForChild("Assets", 30)
local miscFolder = assets:FindFirstChild("Misc")
local eventsFolder = assets:FindFirstChild("Events")
local selectionButtons = miscFolder:FindFirstChild("SelectionButtons")

local selectionMenu = script.Parent.SelectionMenu
local mouseLocation1 = Vector2.new()
local mouseLocation2 = Vector2.new()
local cursor = selectionMenu.Cursor
local coolDown = 0

local tweenService = game:GetService("TweenService")
local tween = TweenInfo.new(0.1)

local selectedButton = ""

local close = selectionMenu.Close

local textTransparency
local uisTransparency

if selectionButtons then
	(function()
		local buttonCount = #selectionButtons:GetChildren()
		local position = math.pi * 2 / buttonCount
		
		for index, value in pairs(selectionButtons:GetChildren()) do
			local buttonOrder = (value:GetAttribute("Order") and index) - 1
			local button = script.Button:Clone()
			
			button.Size = UDim2.fromScale(1 / math.max(math.round(buttonCount / 2.5), 3), 1)
			button.Position = UDim2.fromScale(0.5 + math.cos(position * buttonOrder) * -0.5, 0.5 + math.sin(position * buttonOrder) * -0.5)
			button.Label.Text = value.Name
			button.Name = value.Name
			button.Parent = selectionMenu.Buttons
		end
	end)()
end

local function initialize()
	selectionMenu.Visible = not selectionMenu.Visible
	--userinputService.MouseIconEnabled = not selectionMenu.Visible;

	if not selectionMenu.Visible then
		pcall(function()
			runService:UnbindFromRenderStep("radialMenu")
		end)
		mouseLocation2 = Vector2.new()
		return
	end
	
	mouseLocation1 = userinputService:GetMouseLocation()
	
	game:GetService("RunService"):BindToRenderStep("radialMenu", Enum.RenderPriority.Last.Value, function()
		userinputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		selectionMenu.Timer.Text = math.max(coolDown - os.time(), 0) .. " seconds"
		
		if not (coolDown <= os.time()) then
			uisTransparency = 0.5
		else
			uisTransparency = 0.9
		end
		selectionMenu.Timer.UIStroke.Transparency = uisTransparency
		
		if not (coolDown <= os.time()) then
			textTransparency = 0
		else
			textTransparency = 0.9
		end
		selectionMenu.Timer.TextTransparency = textTransparency
		 
		mouseLocation1 = userinputService:GetMouseLocation()
		mouseLocation2 = mouseLocation2 + (userinputService:GetMouseLocation() - mouseLocation1) / 2

		--print(mouseLocation2)
		if mouseLocation2.Magnitude > 1 then
			mouseLocation2 = mouseLocation2.Unit
		end
		cursor.Position = UDim2.fromScale(0.5 + mouseLocation2.X * 0.5, 0.5 + mouseLocation2.Y * 0.5);

		if math.abs(mouseLocation2.Magnitude) >= 0.85 then
			
			local magnitude = math.huge
			local selection
			for index, value in pairs(script.Parent.SelectionMenu.Buttons:GetChildren()) do
				if value:IsA("Frame") then
					local pos = value.AbsolutePosition + value.AbsoluteSize / 2
					local pos2 = cursor.AbsolutePosition + cursor.AbsoluteSize / 2
					if (pos - pos2).Magnitude < magnitude then
						magnitude = (pos - pos2).Magnitude
						selection = value
					end
				end
			end
			for index, value in pairs(script.Parent.SelectionMenu.Buttons:GetChildren()) do
				if value:IsA("Frame") then
					tweenService:Create(value.Glow, tween, { Size = UDim2.fromScale(1.25, 1.25), ImageColor3 = Color3.fromRGB(0, 0, 0) }):Play()
				end
			end
			
			if selection then
				tweenService:Create(selection.Glow, tween, {Size = UDim2.fromScale(1.5, 1.5), ImageColor3 = Color3.fromRGB(0, 170, 0)}):Play()
				selectedButton = selection.Name
			end
			
			tweenService:Create(close.Glow, tween, { Size = UDim2.fromScale(1.25, 1.25), ImageColor3 = Color3.fromRGB(0, 0, 0)}):Play()
		else
			for index, value in pairs(script.Parent.SelectionMenu.Buttons:GetChildren()) do
				if value:IsA("Frame") then
					tweenService:Create(value.Glow, tween, {Size = UDim2.fromScale(1.25, 1.25), ImageColor3 = Color3.fromRGB(0, 0, 0)}):Play()
				end
				tweenService:Create(close.Glow, tween, {Size = UDim2.fromScale(1.5, 1.5), ImageColor3 = Color3.fromRGB(170, 0, 0)}):Play()
				selectedButton = "Close"
			end

		end
		selectionMenu.Selected.Text = selectedButton
	end)
	print(userinputService.MouseBehavior)
end

userinputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if selectionButtons then
		if inputObject.KeyCode == Enum.KeyCode.B and not gameProcessedEvent then
			initialize()
		elseif inputObject.UserInputType == Enum.UserInputType.MouseButton1 and selectionMenu.Visible then
			if selectedButton == "Close" then
				initialize()
			elseif coolDown <= os.time() then
				local returnedData = game:GetService("ReplicatedStorage").PlaySound:InvokeServer(selectedButton)
				if returnedData ~= 0 then
					print(returnedData)
					coolDown = returnedData
					initialize()
				end
			end
		end
		
	end
end)
2 Likes

I think I know the problem:

  1. mouseLocation2 should be set on the next frame, not on the same frame mouseLocation1 is set, since then there would be no MouseDelta since the DeltaTime between both is 0.

  2. You can also use +=, -=, *=, /=, unless performance reasons.
    for index,value in pairs(table)do can also be for index,value in next, table do or for index,value in table do. I’m saying it since it may improve readability or help you, but if you don’t care, ignore this.

  3. I’d rather say you don’t need to add mouse location to old mouse location, you have to substract to get the distance from both.

Feels weird how the radial menu uses those buttons,feels like a ‘retro’ aesthetic, but what can I say, you said a friend of yours sent it :person_shrugging:
2 Likes

Thank you! I didn’t even think about that.:sob:
Do you know how I could slow it down a bit? It is hard to center it and hit the close button.

1 Like

maybe dividing it by more? (if it gets faster try multiplying instead)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.