My spray paint cant stop stacking (RAYCAST)

i wanted to make a can spray on roblox, but the paint keeps stacking. I tried adding an ignore list system but it didnt work out as i expected.

my code:

local mouse = game.Players.LocalPlayer:GetMouse()
local isMouseOnHold = false
local isEquipped = false


function getAngleBetween(u, v, axis)
	local dot, uxv = u:Dot(v), u:Cross(v)
	if dot < -0.99999 then return CFrame.fromAxisAngle(axis, math.pi) end
	return CFrame.new(0,0,0, uxv.x, uxv.y, uxv.z, 1+dot)
end

script.Parent.Unequipped:Connect(function()
	isEquipped = false
end)

script.Parent.Equipped:Connect(function()
	isEquipped = true
	mouse.Button1Down:Connect(function()
		isMouseOnHold = true
	end)
	mouse.Button1Up:Connect(function()
		isMouseOnHold = false
	end)
end)

local paints = {}

mouse.Move:Connect(function()
	if isMouseOnHold and isEquipped then
		local ro = workspace.CurrentCamera.CFrame.Position
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = paints
		local raycastResults = workspace:Raycast(ro, mouse.UnitRay.Direction*1000, raycastParams)
		if raycastResults then
			local p = Instance.new("Part", workspace.Debris)
			p.Size = Vector3.new(1,0.25,1)
			p.Anchored = true
			p.CFrame = getAngleBetween(p.CFrame.UpVector, raycastResults.Normal, ro)
			p.Position = mouse.Hit.Position
			p.CanCollide = false
			table.insert(paints, p)
		end
	end
end)

can someone tell me what’s wrong?

I do raycasting a lot, and i think that you shouldn’t really use a table to separate what your raycast will detect. using FilterDescendantsInstances for folders is better if you may ask me

local paints = workshop.Debris

mouse.Move:Connect(function()
	if isMouseOnHold and isEquipped then
		local ro = workspace.CurrentCamera.CFrame.Position
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {paints} -- you can add character in here if you want separate them from the raycast too
		local raycastResults = workspace:Raycast(ro, mouse.UnitRay.Direction*1000, raycastParams)
		if raycastResults then
			local p = Instance.new("Part", workspace.Debris)
			p.Size = Vector3.new(1,0.25,1)
			p.Anchored = true
			p.CFrame = getAngleBetween(p.CFrame.UpVector, raycastResults.Normal, ro)
			p.Position = mouse.Hit.Position
			p.CanCollide = false
		end
	end
end)


it is still stacked

local mouse = game.Players.LocalPlayer:GetMouse()
local isMouseOnHold = false
local isEquipped = false


function getAngleBetween(u, v, axis)
	local dot, uxv = u:Dot(v), u:Cross(v)
	if dot < -0.99999 then return CFrame.fromAxisAngle(axis, math.pi) end
	return CFrame.new(0,0,0, uxv.x, uxv.y, uxv.z, 1+dot)
end

script.Parent.Unequipped:Connect(function()
	isEquipped = false
end)

script.Parent.Equipped:Connect(function()
	isEquipped = true
	mouse.Button1Down:Connect(function()
		isMouseOnHold = true
	end)
	mouse.Button1Up:Connect(function()
		isMouseOnHold = false
	end)
end)

local paints = workspace.Debris

mouse.Move:Connect(function()
	if isMouseOnHold and isEquipped then
		local ro = workspace.CurrentCamera.CFrame.Position
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {paints} -- you can add character in here if you want separate them from the raycast too
		local raycastResults = workspace:Raycast(ro, mouse.UnitRay.Direction*1000, raycastParams)
		if raycastResults then
			local p = Instance.new("Part", workspace.Debris)
			p.Size = Vector3.new(1,0.25,1)
			p.Anchored = true
			p.CFrame = getAngleBetween(p.CFrame.UpVector, raycastResults.Normal, ro)
			p.Position = raycastResults.Position
			p.CanCollide = false
		end
	end
end)

nvm i fixed it, the p.position is using the mouse position

local mouse = game.Players.LocalPlayer:GetMouse()
local isMouseOnHold = false
local isEquipped = false

function getAngleBetween(u, v, axis)
	local dot, uxv = u:Dot(v), u:Cross(v)
	if dot < -0.99999 then return CFrame.fromAxisAngle(axis, math.pi) end
	return CFrame.new(0,0,0, uxv.x, uxv.y, uxv.z, 1+dot)
end

script.Parent.Unequipped:Connect(function()
	isEquipped = false
end)

script.Parent.Equipped:Connect(function()
	isEquipped = true
	mouse.Button1Down:Connect(function()
		isMouseOnHold = true
	end)
	mouse.Button1Up:Connect(function()
		isMouseOnHold = false
	end)
end)

local paints = {}

mouse.Move:Connect(function()
	if isMouseOnHold and isEquipped then
		local ro = workspace.CurrentCamera.CFrame.Position
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = paints
		local raycastResults = workspace:Raycast(ro, mouse.UnitRay.Direction*1000, raycastParams)
		if raycastResults and not ( raycastResults.Instance:HasTag("Paint") ) then
			local p = Instance.new("Part", workspace.Debris)
			p.Size = Vector3.new(1,0.25,1)
			p.Anchored = true
			p.CFrame = getAngleBetween(p.CFrame.UpVector, raycastResults.Normal, ro)
			p.Position = mouse.Hit.Position
			p.CanCollide = false
			p:AddTag("Paint")
		end
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.