How to make a wall spray painting tool?

I’m creating a game where players can come and spray paint the walls of the buildings. I’m still testing it out, however, I came upon a small issue. The “paint” parts are stacking on top of each other.

Here is what I did to create this:

local ray = Ray.new(Camera.CFrame.Position, (Mouse.Hit.Position - Camera.CFrame.Position).Unit * 300, Player.Character)
local part, pos, normal = workspace:FindPartOnRay(ray)

-- Wall Painting here
local p = Instance.new("Part")
		p.FormFactor = "Custom"
		p.Size = Vector3.new(0.5, 0.5, 0.2)
		p.CanCollide = false
		p.Anchored = true
		p.Parent = workspace
		p.CFrame = CFrame.new(pos, pos + normal)
2 Likes

Use workspace:Raycast() instead of a Ray object. There you can use RaycastParams and blacklist the spray paint.

Any tips on converting my code over to workspace:Raycast()?

Here’s how you would do it:

local userInputService = game:GetService("UserInputService")

local mouseVP = userInputService:GetMouseLocation()
local mouseRay = workspace.CurrentCamera:ViewportPointToRay(mouseVP.x, mouseVP.y)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {workspace.SprayPaint} -- Put the new spray paint parts in that model so it can ignore them

local result = workspace:Raycast(mouseRay.Origin, mouseRay.Unit.Direction * 300, params)

The hit part would be result.Instance and the intersection position result.Position.

Also, if you want the spray paint to be visible for everyone, you should make the client fire to a remote event and create the part there.

3 Likes