Need help with raycasting bug

  1. What do you want to achieve? I’m attempting to setup a placing system by following various tutorials, but I’ve come across a bug that causes the ‘clientStructure’ to clip up and down for the first few seconds, as well as the raycast hit position slowly ticks up. Not sure if they are related or not.

  2. What is the issue?

  3. What solutions have you tried so far? I’ve tried debugging the script with dozens of print lines, and I think I’ve narrowed it down to the raycast hit position slowly ticking up. But I’m unsure why it’s doing that.

Local Placement Handler

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceRF = ReplicatedStorage:WaitForChild("Place")
local Structures = ReplicatedStorage:WaitForChild("Structures")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local mouse = player:GetMouse()

local yBuildingOffset = 5
local maxPlacingDistance = 50
local rKeyIsPressed = false
local placingStructure = false

local move1
local move2
local rotation
local rotation2
local click

local function Raycast(X, Y, ignoreList)
	local Cam = workspace.CurrentCamera

	local Ray = Cam:ScreenPointToRay(X, Y)

	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.FilterDescendantsInstances = ignoreList

	local Raycast = workspace:Raycast(Ray.Origin, Ray.Direction * 500, Params)
	
	if not Raycast then return nil end
	print(Raycast.Instance, Raycast.Position)
	
	return Raycast.Instance, Raycast.Position
end

for _, structureButton in pairs(StructureFrame:GetChildren()) do
	if structureButton:IsA("TextButton") then
		structureButton.MouseButton1Click:Connect(function()

			StructureFrame.Visible = false
			
			local yOrientation = 0
			local goodToPlace = false
			local placedStructure

			if placingStructure == false then
				placingStructure = true

				local clientStructure = Structures:FindFirstChild(structureButton.Name):Clone()
				clientStructure.BrickColor = BrickColor.new("Forest green")
				clientStructure.Material = "Neon"
				clientStructure.CanCollide = false
				clientStructure.Parent = game.Workspace
				
				move1 = UIS.InputBegan:Connect(function(input, processed)
					
					if input.UserInputType == Enum.UserInputType.MouseMovement then
						local hit, Pos = Raycast(input.Position.X, input.Position.Y, {clientStructure})
						
					
						if hit and (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDistance then
							goodToPlace = true
							
							clientStructure.BrickColor = BrickColor.new("Forest green")
						else
							goodToPlace = false
							
							clientStructure.BrickColor = BrickColor.new("Bright red")
						end

						local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
						
						local newCFrame = CFrame.new(Pos.X, Pos.Y + yBuildingOffset, Pos.Z)
						
						clientStructure.CFrame = newCFrame * newAnglesCFrame
						
					end
				end)
				
				move2 = UIS.InputChanged:Connect(function(input, processed)
					
					if input.UserInputType == Enum.UserInputType.MouseMovement then
						local hit, Pos = Raycast(input.Position.X, input.Position.Y, {clientStructure})
						

						if hit and (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDistance then
							goodToPlace = true
							
							clientStructure.BrickColor = BrickColor.new("Forest green")
						else
							goodToPlace = false
							
							clientStructure.BrickColor = BrickColor.new("Bright red")
						end
						if Pos then
							local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
							
							local newCFrame = CFrame.new(Pos.X, Pos.Y + yBuildingOffset, Pos.Z)
							
							clientStructure.CFrame = newCFrame * newAnglesCFrame
							
						end
					end
				end)
				
				
				rotation = UIS.InputBegan:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						rKeyIsPressed = true

						local rotationSpeed = 5
						while rKeyIsPressed do
							wait()
							if placingStructure == true then
								yOrientation = yOrientation + rotationSpeed
							end
						end
					end
				end)

				rotation2 = UIS.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						rKeyIsPressed = false
					end
				end)

				click = UIS.InputBegan:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						
						if placingStructure == true then
							if goodToPlace == true then
								local StructureCFrame = clientStructure.CFrame
								
								placedStructure = PlaceRF:InvokeServer(clientStructure.Name, StructureCFrame)
								print(placedStructure)
								if placedStructure == true then
									placingStructure = false
									clientStructure:Destroy()
									StructureFrame.Visible = true
									
									move1:Disconnect()
									move2:Disconnect()
									rotation:Disconnect()
									rotation2:Disconnect()
									click:Disconnect()
								end
							end
						end
					end
				end)
			end
		end)
	end
end
1 Like

I solved it, apparently it’s not a coding bug at all but more of a “I forgot to anchor the part” bug. Whoops!

1 Like