Changing mouse icon when tool out doesn't work if using more than 1 tool

This is a tool script and I want the mouse icon to change when hovering over a tree. The code actually works fine but if I give myself another hatchet the 2nd one’s mouse icon doesn’t change when I hover over any trees. You’re not supposed to have 2 hachets in my game but I want to use the same code for different types of axes so I’ll need to fix this anyway

Sorry if its messy I’m not the best scripter and I’ve been trying to get better looking at old forum posts, none had the same problem as me.

local hatchet = script.Parent
local swinging = false
local UIS = game:GetService("UserInputService")
local equipped = false


hatchet.Equipped:Connect(function()
	equipped = true
end)

hatchet.Unequipped:Connect(function()
	equipped = false
end)

UIS.InputChanged:Connect(function() --changes mouse icon while hovering on tree
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	if mouse.Target and equipped then -- The "if mouse.Target" ensures the target is not nil
		local distance = (player.Character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
		local tree_alive = false
		if mouse.Target.Parent:FindFirstChild("Alive") then
			tree_alive = mouse.Target.Parent.Alive.Value
		end
		if mouse.Target.Name == "Trunk" and mouse.Target.Parent.Parent.Name == "CuttingSystem" and distance <= 10 and tree_alive then
			mouse.Icon = 'rbxassetid://13222916056'
		else
			mouse.Icon = ""
		end
	else
		mouse.Icon = ""
	end
end)

You have to create a RBXScriptConnection for the UIS.InputChanged event when equipping the tool and disconnect it when unequipping the tool. This way the function which is connected to the event UIS.InputChanged won’t be run when the tool is unequipped.

You could for example use the following:

local hatchet = script.Parent
local swinging = false
local UIS = game:GetService("UserInputService")
local equipped = false

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

-- Initialize RBXScriptConnection variable for connecting to UIS.InputChanged event
local UpdateIconConnection: RBXScriptConnection = nil

-- Function that gets run when UIS.InputChanged event fires
local function UpdateMouseIcon()
	if mouse.Target and equipped then -- The "if mouse.Target" ensures the target is not nil
		local distance = (player.Character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
		local tree_alive = false
		if mouse.Target.Parent:FindFirstChild("Alive") then
			tree_alive = mouse.Target.Parent.Alive.Value
		end
		if mouse.Target.Name == "Trunk" and mouse.Target.Parent.Parent.Name == "CuttingSystem" and distance <= 10 and tree_alive then
			mouse.Icon = 'rbxassetid://13222916056'
		else
			mouse.Icon = ""
		end
	else
		mouse.Icon = ""
	end
end

hatchet.Equipped:Connect(function()
	-- Create connection
	UpdateIconConnection = UIS.InputChanged:Connect(UpdateMouseIcon)

	equipped = true
end)

hatchet.Unequipped:Connect(function()
	-- Disconnect connection if it exists and is connected
	if UpdateIconConnection and UpdateIconConnection.Connected == true then
		UpdateIconConnection:Disconnect()
	end

	equipped = false
end)

Some other things to note are that you should initialize the variables player and mouse at the beginning of the script rather than every time the UIS.InputChanged event gets fired.

1 Like

Yep, it works thanks a lot bud. Also thanks for telling me about making unnecessary variables in the UIS event I don’t wanna make that a habit I wasn’t even thinking.

1 Like

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