Hi! I want to make a system that puts a part with a decal on it on a surface that I click on. It works for the Z and Y axis, but it doesn't work for the X axis. Can anyone help me? This is the explanation:

https://imgur.com/a/HGmuZhi

This is the code:

local UIS = game:GetService("UserInputService")

local mouse = game.Players.LocalPlayer:GetMouse()

local player = game.Players.LocalPlayer

local character = player.Character

local humanoid = character:WaitForChild("Humanoid")

local SprayPoints = game.Workspace:GetDescendants()

local Equipped = false

local canSpray = true

local spraying = false

local maxDistance = 10

local SpraySound = Instance.new("Sound", script.Parent)

SpraySound.Name = "SpraySound"

SpraySound.SoundId = "https://www.roblox.com/asset/?id=843338638"

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	if input.KeyCode == Enum.KeyCode.E and canSpray then
		if Equipped == false then
			Equipped = true
			print("Equipped is ", Equipped)
		else
			Equipped = false
			print("Equipped is ", Equipped)
		end
	end
	
	mouse.Button1Down:Connect(function()
		if Equipped then
			print("Pressed")
			local target = mouse.Target
			print(target.Name)
			if (character.UpperTorso.Position - mouse.Hit.p).magnitude <= maxDistance and spraying == false then
				canSpray = true
			else
				canSpray = false
			end
			if target and target.Name == "Wall" and spraying == false and canSpray then
				canSpray = false
				spraying = true
				SpraySound:Play()
				wait()
				local SprayPart = Instance.new("Part", workspace)
				SprayPart.Anchored = true
				SprayPart.Name = "SprayPart"
				SprayPart.Transparency = 1
				SprayPart.CFrame = CFrame.new(target.Position, Vector3.new(target.Position.X, target.Position.Y, script.Parent.HumanoidRootPart.Position.Z))
				SprayPart.Size = Vector3.new(10, 7.75, 0.25)
				SprayPart.Position = mouse.Hit.p
				local SprayTexture = Instance.new("Decal")
				SprayTexture.Name = "SprayTexture"
				SprayTexture.Texture = "http://www.roblox.com/asset/?id=4553208328"
				SprayTexture.Parent = SprayPart
				spraying = false
				target = nil
				wait(5)
				canSpray = true
			end
		end
	end)
end)

1 comment

0

try instead firing a ray from the spraycan and putting the part where the :FindPartOnRay() function hits and as for the way it faces you can grab the part that it hits rotation