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

Having The Mouse Hovering Over A Grid?

Basically I've created a grid with 36 separate blocks and grouped them together in a model named "Grid" I managed to get over my first obstacle with the help of someone(Rheines) by making a script that would place a block above a section of the grid if the player clicks down on that section, woohoo! So the problem I've encountered is I actually hoped to change it so when the player is hovering over a section of the grid the part displays on that section until the cursor moves to another section in-which it would disappear from the previous and move to the new.

I thought I could do this using a while true loop having it constantly check the position of the players cursor, and then placing a block above that section but I don't know how I'd go about making it so when the player moves to a new section it removes the old block?

local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse()

while true do

wait()
local Origin = Player.Character:WaitForChild("Torso")
local ray = Ray.new(Origin.Position, (Mouse.Hit.p - Origin.Position).unit * 500)
local part, hitPosition = workspace:FindPartOnRay(ray, Player.Character)
if part then
    if part.Parent.Name == "Grid" then
        local Brick = script["Brick1"]:Clone()
		Brick.CFrame = part.CFrame*CFrame.new(0,2,0)
		Brick.Parent = game.Workspace
    end
end

end

1 answer

✓ Accepted answer

You can check if a part already generates a brick, then you can reference that brick and the part it created. If it is hovering over the same part, do nothing. If it is hovering over a different part, delete the part in the old grid and create a new one in place.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

--References to the last part the player hovers.
local LastPart = nil
local SelectedBrick = nil



while true do
	wait()
	--Reupdate mouse target filter so that you can detect a part through the created brick.
	Mouse.TargetFilter =  LastPart
	local Origin = Player.Character:WaitForChild("Torso")
	local ray = Ray.new(Origin.Position, (Mouse.Hit.p - Origin.Position).unit * 500)
	--Use ignore list so the ray will ignore the cloned part.
	local part, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, {Player.Character, LastPart})
	if part then
	    if part.Parent.Name == "Grid" then		
			--[[If it is the first time the player hovers over a grid,
			create the brick in position, and set a reference that it holds the last part.]]
			if SelectedBrick == nil then
				SelectedBrick = part
				local Brick = script["Brick1"]:Clone()
				Brick.CFrame = part.CFrame*CFrame.new(0,2,0)
				Brick.Parent = workspace
				LastPart = Brick
				
			--If the player is hovering over the same brick do nothing.
			elseif SelectedBrick == part then
				
			--If it is a different grid and is not the first one then delete the last part.
			else
				SelectedBrick = part
				LastPart:Destroy()
				local Brick = script["Brick1"]:Clone()
				Brick.CFrame = part.CFrame*CFrame.new(0,2,0)
				Brick.Parent = workspace
				LastPart = Brick 
			end
	    end
	end
end

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