PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

TargetSurface

Mouse

NormalId

This property indicates the Enum/NormalId of the BasePart surface at which the mouse is pointing. This property is derived from the world position of mouse (Mouse/Hit) and the part toward which the mouse is pointing (Mouse/Target).

This property isn’t meaningful when the mouse is not pointing at a part, for example when the mouse is pointing at the sky. At the moment, this property is set to ‘Right’ under these circumstances. Before using this property, check that the mouse’s target is not nil.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Check that there exists a part at which the mouse is pointing
if mouse.Target then
	print("The mouse is pointing to the " .. mouse.TargetSurface.Name .. " side of " .. mouse.Target.Name)
else
	print("The mouse is not pointing at anything.")
end


Code Samples


Surface Randomizer

The code in this sample, when placed in a LocalScript inside StarterPlayerScripts will set the surface of any BasePart clicked on to a random surface.

local Players = game:GetService("Players")

-- grab the mouse
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()

-- define surface types 
local surfaceTypes = {
	Enum.SurfaceType.Smooth,
	Enum.SurfaceType.Glue,
	Enum.SurfaceType.Weld,
	Enum.SurfaceType.Studs,
	Enum.SurfaceType.Inlet,
	Enum.SurfaceType.Universal,
	Enum.SurfaceType.Hinge,
	Enum.SurfaceType.Motor
}

-- listen for mouse clicks
mouse.Button1Down:Connect(function()
	-- make sure the mouse is pointing at a part
	local target = mouse.Target
	if not target then
		return
	end

	-- pick a random surface type
	local surfaceType = surfaceTypes[math.random(1, #surfaceTypes)]

	-- find the correct surface
	local surface = mouse.TargetSurface 
	local propertyName = surface.Name.."Surface"

	-- set the surface type
	mouse.Target[propertyName] = surfaceType
end)