Code not functioning with no errors

Lately, I’ve been trying to make a delete system but I can’t seem to find the way to tweak it. It has no errors and no trace. I assume there’s something wrong with the: if mouse.Hit.Position == workspace.FrontRoof.Position then

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Delete.MouseButton1Click:Connect(function()
if mouse.Hit.Position == workspace.FrontRoof.Position then
local SB = Instance.new(“SelectionBox”)
SB.Adornee = workspace.FrontRoof
SB.Color3 = Color3.new(255, 0, 0)
SB.Parent = workspace.FrontRoof
print(mouse.Target.Name)
end
end)

1 Like

I reccommend using mouse.Target like this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Delete.MouseButton1Click:Connect(function()
if mouse.Target == workspace.FrontRoof then --instead of mouse.Hit.Position it's mouse.Target
local SB = Instance.new(“SelectionBox”)
SB.Adornee = workspace.FrontRoof
SB.Color3 = Color3.new(255, 0, 0)
SB.Parent = workspace.FrontRoof
print(mouse.Target.Name)
end
end)

It’s not working because the position of the mouse isn’t the same as FrontRoof yea sure you aimed it exactly at it but you’re typically just hitting it let’s say 1 stud away from being the same position, the solution is to use magnitude(get the range of roof and mouse target position) or as @Kaid3n22 suggest, get the target instance and compare it to the FrontRoof Instance/Object.

1 Like

Also, Color3.new takes numbers from 0 to 1, you want to either change this to 1,0,0 or use Color3.fromRgb

Same issue:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Delete.MouseButton1Click:Connect(function()
if mouse.Target == workspace.FrontRoof then --instead of mouse.Hit.Position it’s mouse.Target
local SB = Instance.new(“SelectionBox”)
SB.Adornee = workspace.FrontRoof
SB.Color3 = Color3.new(255, 0, 0)
SB.Parent = workspace.FrontRoof
print(mouse.Target.Name)
end
end)

I’m new to magnitude, can you tell me how I could use it?

What is script.Parent.Delete? It doesn’t look right.

I’m pretty sure that you cant hover on a part while clicking a button (unless its mobile).
Also most times your mouse can’t be perfectly on the part.

Maybe try this?

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

local SB = Instance.new("SelectionBox")
SB.Color3 = Color3.new(255, 0, 0)
SB.Parent = workspace

local deleting = false

mouse.Move:Connect(function()
	SB.Adornee = mouse.Target
end)

mouse.Button1Down:Connect(function()
	if mouse.Target ~= workspace.FrontRoof or not deleting then
		return
	end
	
	deleting = false
	mouse.Target:Destroy()
end)

script.Parent.Delete.MouseButton1Click:Connect(function()
	deleting = not deleting
end)

https://gyazo.com/47e522b278bb8bfc0804f27163ac14c2

2 Likes

It’s the textbutton that I’m using.