I am trying to make an advanced building tool. I came to the display function that would show the hologram of a chosen part in world space. For that I am trying to use rounded mouse position and Look/Up/Right Vectors of Mouse.Target
as well as using the Mouse.TargetSurface
.
The problem is, my display function suddenly stops working when I try to use “if” statements. Here is a code with them included:
local UserInputService = game:GetService("UserInputService")
local players = game.Players
local player = players.LocalPlayer
local tool = script.Parent
local block = tool.Block
local holo = tool.BlockHolo
local mouse = player:GetMouse()
mouse.Move:Connect(function() -- Display
local mousePosition = Vector3.new(math.round(mouse.Hit.Position.X), math.round(mouse.Hit.Position.Y), math.round(mouse.Hit.Position.Z))
local target = mouse.Target
local targetSurface = mouse.TargetSurface
if targetSurface == "TopSurface" then
holo.Position = mousePosition + target.CFrame.UpVector
elseif targetSurface == "BottomSurface" then
holo.Position = mousePosition - target.CFrame.UpVector
end
end)
It practically should determine top or bottom surfaces of the target and use negated or regular UpVector to move the holo slightly away from the part. The issue is, when trying to actually see the hologram move, it does not work at all, but for some strange reason if I do the same code but without “if” statement
local UserInputService = game:GetService("UserInputService")
local players = game.Players
local player = players.LocalPlayer
local tool = script.Parent
local block = tool.Block
local holo = tool.BlockHolo
local mouse = player:GetMouse()
mouse.Move:Connect(function() -- Display
local mousePosition = Vector3.new(math.round(mouse.Hit.Position.X), math.round(mouse.Hit.Position.Y), math.round(mouse.Hit.Position.Z))
local target = mouse.Target
local targetSurface = mouse.TargetSurface
holo.Position = mousePosition + target.CFrame.UpVector
end)
The hologram will actually move. I don’t understand what possibly could have gone wrong, and the code does not seem to have any errors, so I desperately need help.