How do I detect whether my cursor can click or not

Hey there,

So I want to make a classic tycoon game that features classic gui’s and appearances, I however stumbled upon a problem.

In the “classic” hub the cursor can click parts that contain clickdetectors.

My cursor however lacks that feature.

My script:

local resetPlayerGui = false

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local char = nil

local diskOrigin = script:WaitForChild("Disk")
local disk
local distance = 55
local enabled = true

local cursor = script.Parent:WaitForChild("Cursor")
cursor.Visible = true

local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")

local pr = nil
local newDisk = diskOrigin:Clone()
if not resetPlayerGui then
	if game:GetService("Workspace").CurrentCamera:FindFirstChild("Disk") then
		game:GetService("Workspace").CurrentCamera.Disk:Destroy()
	end
end
newDisk.Parent = game:GetService("Workspace").CurrentCamera
disk = newDisk

local keys = {}

while char == nil do
	local t = wait(1/30)
	char = player.Character
end

if resetPlayerGui then
	function defineChar()
		while char == nil do
			local t = wait(1/30)
			char = player.Character
		end
	end
	defineChar()
	player.CharacterAdded:connect(function() char = nil defineChar() end)
end

userInputService.MouseIconEnabled = false

function findTool()
	if char then
		local children = char:GetChildren()
		for i = 1, #children do
			if children[i]:IsA("Tool") then
				return true
			end
		end
		return false
	end
end

mouse.KeyDown:connect(function(key)
	keys[key:lower()] = true
	if key:byte() == 17 then
		keys["w"] = true
	end
	if key:byte() == 20 then
		keys["a"] = true
	end
	if key:byte() == 18 then
		keys["s"] = true
	end
	if key:byte() == 19 then
		keys["d"] = true
	end
end)
mouse.KeyUp:connect(function(key)
	keys[key:lower()] = false
	if key:byte() == 17 then
		keys["w"] = false
	end
	if key:byte() == 20 then
		keys["a"] = false
	end
	if key:byte() == 18 then
		keys["s"] = false
	end
	if key:byte() == 19 then
		keys["d"] = false
	end
end)

mouse.Button1Down:connect(function()
	if enabled then
		local pos = mouse.Hit.p
		
		if pr then
			pr:Destroy()
		end
		pr = diskOrigin:Clone()
		pr.Parent = game:GetService("Workspace").CurrentCamera
		pr.CFrame = CFrame.new(pos)
		
		if char and char:FindFirstChild("Humanoid") then
			char.Humanoid:MoveTo(pr.Position, pr)
		end
	end
end)

runService.RenderStepped:connect(function()
	cursor.Position = UDim2.new(0, mouse.X - cursor.AbsoluteSize.X/2, 0, mouse.Y - cursor.AbsoluteSize.Y/2)
	if findTool() then
		enabled = false
		cursor.Image = ""
		userInputService.MouseIconEnabled = true
	else
		mouse.TargetFilter = disk
		cursor.Size = UDim2.new(0, 128, 0, 128)
		userInputService.MouseIconEnabled = false
		if enabled then
			cursor.Image = "rbxassetid://172802980"
		else
			cursor.Image = "rbxassetid://167166957"
		end
	end
	disk.Parent = enabled and game:GetService("Workspace").CurrentCamera or game:GetService("Lighting")
	if keys["w"] or keys["a"] or keys["s"] or keys["d"] then
		if pr then
			pr:Destroy()
		end
	end
	if pr then
		if char and char:FindFirstChild("Torso") then
			local mag = (char.Torso.Position - pr.Position).magnitude
			if math.floor(mag) <= 3 then
				pr:Destroy()
			end
		end
	end
end)

while true do
	disk.CFrame = CFrame.new(mouse.Hit.p)
	if char and char:FindFirstChild("Torso") then
		if (char.Torso.CFrame.p - mouse.Hit.p).magnitude > distance then
			enabled = false
		else
			if not findTool() then
				enabled = true
			end
		end
	end
	local t = wait(1/60)
end

thank you for helping in advance.

Is there a reason that you aren’t setting the cursor with Mouse.Icon?

That is because the cursor would change to normal if I were to hover over something I could click. setting mouse.icon on itself isn’t reliable as it will change.

I see what you mean. In that case, you could check Mouse.Target each frame.

if mouse.Target and mouse.Target:FindFirstChildOfClass("ClickDetector") then
	-- Clickable
end

I didn’t know that was a thing.

Thank you a lot, I will try this out in a minute.

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