MouseHoverEnter event issue

Hey all!

I am trying to make a grid that, when a player’s mouse hovers over any particular tile, the tile lights up. In order to accomplish this I have a remove event that fires, in order to create an instance of the selection part locally. However, I am consistently getting the error message “Unable to cast value to Object” from my onMouseHoverEnter function.

The script found within every tile on the board:

local Tile = script.Parent
local Tiles = game.Workspace.Board.Tiles
local tileNum = Tile:GetAttribute("tileNum")
local tileSelect = game.ReplicatedStorage:WaitForChild("tileSelect")

local ClickDetector = Instance.new("ClickDetector")
ClickDetector.MaxActivationDistance = 100
ClickDetector.Parent = Tile

local function onClicked(player)
	
	getMovementTiles(tileNum)
	
end

local function onMouseHoverEnter(player, tileNum)
	
	tileSelect:FireClient(player, tileNum, true)
	
end

local function onMouseHoverLeave(player, tileNum)
	
	tileSelect:FireClient(player, tileNum, false)
	
end

-- Events

ClickDetector.MouseClick:Connect(onClicked)
ClickDetector.MouseHoverEnter:Connect(onMouseHoverEnter(tileNum))
ClickDetector.MouseHoverLeave:Connect(onMouseHoverLeave(tileNum))

The script found within StarterPlayerScripts:

local tileSelect = game.ReplicatedStorage:WaitForChild("tileSelect")
local Tiles = game.Workspace.Board.Tiles
local selection = game.ReplicatedStorage:WaitForChild("selection")

local function tileFromTileNum(tileNum)
	
	print(tileNum)
	
	for i, tile in pairs(Tiles:GetChildren()) do
		
		if tile:GetAttribute("tileNum") == tileNum then
			
			return tile
			
		end
		
	end
	
end


local function onTileSelect (tileNum, bool)
	
	local tile = tileFromTileNum(tileNum)
	
	if bool == true then
		
		local selectionCopy = selection:Clone()
		
		selectionCopy.Position = Vector3.new(tile.Position.X, 70.025, tile.Position.Z)
		
		selectionCopy.Parent = tile
		
		
	else
		
		for i, v in pairs(tile:GetChildren()) do
			
			if v.Name == "selection" then
				
				v:Destroy()
				break
				
			end
			
		end
		
		
	end
	
end

tileSelect.OnClientEvent:Connect(onTileSelect)

*Each tile has a tileNum which handles some of the logic

You just need to pass in a function which will automatically receive the args and not call the function (which will return nil btw)

ClickDetector.MouseHoverEnter:Connect(onMouseHoverEnter)
ClickDetector.MouseHoverLeave:Connect(onMouseHoverLeave)
1 Like

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