Can't figure out if "if" statements affect code anyhow

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.

1 Like

You may have to use .Name. Try printing this before the if to see what your Output reads when you test it:

    print("targetSurface = ", targetSurface, "    targetSurface.Name = ", targetSurface.Name)
	if targetSurface == "TopSurface" then`


Sadly, it did not move the part. Yet at least it does print the target surface name

From what I found on the devforum TargetSurface returns an Enum, not a string.

So should I use if targetSurface.Enum.Value or something like that? Because I am not really familiar with enums

if targetSurface == Enum.NormalId.???? then
2 Likes

I will try that right now, thanks

It does indeed work as it was intended!
image
I am very thankful for your help! :heart_eyes:

No problem. Welcome to the devforum.

Nice work @AIRBORNENicuYT

Just a question, what did the first half of the print statement I gave you say with both variables? Your output window picture only shows the last half of the print statement.

When I saw that your solution did not work, I removed it immediately. But it did give me the name of the surface in a single word being “Top”, “Bottom” and such

It wasn’t actually a solution, it was meant to tell us more information to be able to help.
As @AIRBORNENicuYT mentioned, the reason the if wasn’t working was because the 2 variables weren’t the same format.
That’s why I asked to see what both the variables were in the print line, but your picture only included what 1 of them printed as.