Checking if a player used the joystick

I’ve been trying to get the position of the player’s joystick on-screen. I’ve looked all over the forum with no luck, so I took a peak into the Core Gui and found it there.
The problem is, I’m not too sure how I would find out if they touched the joystick or not because of the value types.

Code:

local uis = game:GetService("UserInputService")

uis.TouchStarted:Connect(function(touch, gameProcessed)
	if gameProcessed then return end
	if not uis.TouchEnabled then return end
	
	local touchGui = player.PlayerGui:FindFirstChild("TouchGui")
	if not touchGui then return end
	local tcf = touchGui:FindFirstChild("TouchControlFrame")
	if not tcf then return end
	local dynamicT = tcf:FindFirstChild("DynamicThumbstickFrame")
	local size = dynamicT.Size
	local inputPos = UDim2.fromScale(touch.Position.X, touch.Position.Y)
	print(size, inputPos)
	if (size - inputPos) < 50 then
		print("hit joystick")
	end
end)

Error:
Attempt to compare UDim2 < number
(coming from line if (size - inputPos) < 50 then)

Any help is appreciated!

3 Likes

You could try using InputBegan on the TouchGui frames.

1 Like

hello! (size - inputPos) < 50 is comparing UDim2 with a number . you should instead use
(size.AbsoluteSize-inputPos.AbsolutePosition) <50

also i think the variable size should be the position of the joystick so i recommend using size.AbsolutePosition

1 Like

Nope. Says that" AbsoluteSize is not a valid member of UDim2". Tried comparing with Vector2 values after I found out it was a property of the Frame, with still no luck.

Here is the updated code:

local function checkTouch(touch:InputObject, gameProcessed:boolean)
	if gameProcessed then return end
	if not uis.TouchEnabled then return end
	if touch.UserInputType ~= Enum.UserInputType.Touch then return end
	
	local touchGui = player.PlayerGui:FindFirstChild("TouchGui")
	if not touchGui then return end
	local tcf = touchGui:FindFirstChild("TouchControlFrame")
	if not tcf then return end
	local dynamicT = tcf:FindFirstChild("DynamicThumbstickFrame")
	if not dynamicT then return end
	local size = dynamicT.Size

	if dynamicT.AbsoluteSize - Vector2.new(touch.Position.X, touch.Position.Y) < Vector2.new(50, 50) then
		print("hit joystick")
		return false
	else
		return true
	end
end

uis.InputBegan:Connect(function(input, processed)
	if processed then return end
	local valid
	if uis.TouchEnabled and not uis.MouseEnabled then
		if input.UserInputType == Enum.UserInputType.Touch then
			valid = checkTouch(input, processed)
		end
		if valid then
			processThrow()
		end
	else
		processThrow()
	end
end)
1 Like

oops sorry . It is supposed to be:

local size = dynamicT.AbsolutePosition
local inputPos = touch.Position

if (size - inputPos).Magnitude >= 50 then
--do stuff
end

magnitude basically return the displacement between size and input position

2 Likes

Update: I’ve solved the problem.
I checked the size of the UI within the player, stored it in a variable, and got the touch’s position as a Vector2 using uis.TouchTapInWorld. I then converted it to a UDim2, and compared each scale component with that of the variable. If they touched within that frame (e.g. they are using the joystick), it would not run the code that would’ve run if they didn’t touch it.

Thank you everyone for your help.

1 Like

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