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

Surface Randomizer

Surface Randomizer

Aug 09 2018, 2:21 PM PST

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)