Why won't this script work?

Hello,

I am making a mining game, and I want my pickaxes to have a hover feature which highlights the selected block. Easy, right? Well, somehow my script won’t work when the pickaxe is equipped, but it works when the tool is not equipped.
Here’s the video of it not working:


The script:

local pickaxe = script.Parent

local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()

local char = plr.Character or plr.CharacterAdded:Wait()
--local equipped = false
local miningHighlight = workspace:FindFirstChild("MiningHighlight")
local miningBlocks = workspace.Blocks:GetChildren()
pickaxe.Equipped:Connect(function(mouse)
	for _,block in next,miningBlocks do
		local cd = block.Highlight
		cd.MouseHoverEnter:Connect(function()
			miningHighlight.Adornee = block
			miningHighlight.Enabled = true
			cd.MouseHoverLeave:Connect(function()
				miningHighlight.Adornee = nil
				miningHighlight.Enabled = false
			end)
		end)
	end
end)
1 Like

ClickDetector.MouseHoverEnter will not be triggered if you’re holding a tool. You can try using raycasting; something like this:

-- These are the VARIABLES.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local pickaxe = script.Parent

local hovering: BasePart = nil
local hoverConnection: RBXScriptConnection = nil

-- These are the FUNCTIONS.
local function removeHovering()
    if not hovering then return end

    hovering.Highlight:Destroy()
    hovering = nil
end

-- This is the MAIN SCRIPTING.
pickaxe.Equipped:Connect(function()
    hoverConnection = RunService.Heartbeat:Connect(function()
        local mousePos = UserInputService:GetMouseLocation()
        local ray = camera:ViewportPointToRay(mousePos.X, mousePos.Y, 0)

        local params = RaycastParams.new()
        params.FilterType = Enum.RaycastFilterType.Blacklist
        params.FilterDescendantsInstances = {pickaxe.Parent} -- The character.

        local result = workspace:Raycast(ray.Origin, ray.Direction * 25, params)
        
        if not result or result.Instance.Parent ~= workspace.Blocks then
            removeHovering()
            return
        end

        local block = result.Instance

        if hovering == block then
            return
        end

        removeHovering()

        hovering = block

        local highlight = Instance.new("Highlight")
        -- Adjust the Highlight's properties here.

        highlight.Parent = block
    end)
end)

pickaxe.Unequipped:Connect(function()
    hoverConnection:Disconnect()
    removeHovering()
end)

It doesn’t work

[charrrrrrrrrrrrrrr]

local pickaxe = script.Parent

local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()

local char = plr.Character or plr.CharacterAdded:Wait()
local equipped = false

--local equipped = false

local miningHighlight = workspace:FindFirstChild("MiningHighlight")
local miningBlocks = workspace.Blocks:GetChildren()

	for _,block in next,miningBlocks do
		local cd = block.Highlight
		cd.MouseHoverEnter:Connect(function()
           if equipped then
			miningHighlight.Adornee = block
			miningHighlight.Enabled = true
           end
			cd.MouseHoverLeave:Connect(function()
				miningHighlight.Adornee = nil
				miningHighlight.Enabled = false
			end)
		end)
	end

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

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

1 Like

This might not fix that it doesn’t work in general, but I misspelled something. The first line in the Unequipped event should be hoverConnection:Disconnect(), not hoveringConnection:Disconnect().

1 Like

I know, I fixed that… but the script wont work

It did not work for me at first, too. But now it does. What I did is increased the amount ray.Direction was being multiplied by. Also, I changed params.FilterDescendantInstances to params.FilterDescendantsInstances, if you haven’t already.

It works, thank you all for your efforts

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