Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What would be the right way to use mouse hover?

So i have this grid placement system. I've made it putting a hover script inside EACH BRICK IN THE GRID but it lags the game too much. So i would like to know if its possible to do a multiple hover detector with a few scripts

Movement system: Moves block around if it is not placed yet

local ZPosition = script.Parent.Position.Z
local XPosition = script.Parent.Position.X
local YPosition = script.Parent.Position.Y + 1.019
local Vector = Vector3.new(XPosition, YPosition, ZPosition)

script.Parent.ClickDetector.MouseHoverEnter:connect(function(Player)
   workspace.Lava.Position = Vector
end)

Placement script: Places block

script.Parent.ClickDetector.MouseClick:Connect(function()
	workspace.Lava.Transparency = 0
	workspace.Lava.CanCollide = true
end)

Move cancellator script: Cancels movement if block is placed

workspace.Lava.Changed:Connect(function()
  if workspace.Lava.Transparency == 0 then
  	  script.Parent.Move.Disabled = true
  end
end)

Please have in mind there's the three scripts in EVERY block in the grid. So the game lags

5 comments

0

You do realise you could have a ClickDetector in a Model, so you could have all the parts in a model and the click detector would work on the model

0

but how would it detect each individual hover over each individual brick?

0

Do you know how to use Remotes? You can make this way more efficient using them

0

i do but idk how they would apply to this sittuation

0

fixed the error, try it again

1 answer

✓ Accepted answer

You can have one script that handles all the grid's blocks.

Disregard my last comment, found it difficult to explain how you can use remote events so I came up with another idea.

First, you need to gather all the blocks by inserting them into a table:

local GridModel = workspace.GridModel
local BlockName = "GridBlock" -- name of the grid's blocks

local GridBlocks = {}

function getGridBlocks()
	if GridModel then 
		for i, v in pairs(GridModel:GetChildren()) do 
			if v.Name == BlockName then 
				local block = v
				table.insert(GridBlocks, block)
			end
		end
	end
end
getGridBlocks()

--Once you have all the grid blocks, you can get each block's click detector and do the same thing:

local ClickDetectors = {}

function getDetectors()
	if #GridBlocks >= 1 then 
		for i, v in pairs(GridBlocks) do 
			if v.Name == BlockName then 
				local clickDetector = v:FindFirstChild("ClickDetector") or Instance.new("ClickDetector", v)
				table.insert(ClickDetectors, clickDetector)
			end
		end
	end
end
getDetectors()

--Now that you have all click detectors, you can apply the events:

--Use a loop to gather the detectors:
for i, v in pairs(ClickDetectors) do 
	if v:IsA("ClickDetector") then 
		local detector = v
		detector.MouseHoverEnter:connect(function(player)
			--code
		end)
		detector.MouseClick:connect(function()
			--code
		end)
	end
end

4 comments

0

Now you won't need three scripts for every block, just a ClickDetector

0

looks awesome! but this pops up in the output: "Workspace.Script:37: attempt to call method 'GetChildren' (a nil value)"

0

Oh mb. Try removing the ':GetChildren()' and leave 'ClickDetectors'

0

thanks dude!

This discussion is preserved as it was. New answers, comments, and votes are closed.