Strange output from placement script

I made a pretty basic placement script for models and it works as intended however on the second placement is blows up the output with errors, there are 2 different errors that show up ( output pics bellow) My guess is that it’s for some reason still trying to get the data from the last model but yet it still runs without stopping. any idea how to fix this?

local replicated = game:GetService("ReplicatedStorage")

local structures = replicated:WaitForChild("Structures")

local PlaceSctructure = replicated.Events:WaitForChild("PlaceStructure")

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

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent
local char = player.Character or player.Character:WaitForChild()

local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local mouse = player:GetMouse()

local yBuildingOffset = 5
local MaxPlacement = 50
local rKeyisPressed = false
local placingStructure = false

for _, menuButton in pairs(StructureFrame:GetChildren()) do
	if menuButton:IsA("TextButton") then
		menuButton.MouseButton1Click:Connect(function()
			
			StructureFrame.Visible = false
			
			local yOrientation = 0
			local goodtoplace = false
			local placedstructure
			
			if placingStructure == false then
				placingStructure = true
				
				local Clientstructure = structures:FindFirstChild(menuButton.Name):Clone()
				Clientstructure.Parent = game.Workspace
				
				local startingCFrame = CFrame.new(0, -2, -15)
				Clientstructure:SetPrimaryPartCFrame(HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame))
				
				runserv.RenderStepped:Connect(function()
					if placingStructure == true then
					local mouseRay = mouse.UnitRay
					local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
					local ignoreList = {Clientstructure, char}
					local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
					
						if hit and (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidRootPart.Position - Clientstructure.PrimaryPart.Position).Magnitude < MaxPlacement then
							goodtoplace = true
						else
							goodtoplace = false
						end
					
					local newAngleCframe = CFrame.Angles(0, math.rad(yOrientation), 0)
					local newCframe = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
					Clientstructure:SetPrimaryPartCFrame(newCframe * newAngleCframe)
					end
				end)	
				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)
				UIS.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						rKeyisPressed = false
					end
				end)
				UIS.InputBegan:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						print(goodtoplace)
						if placingStructure == true then
							if goodtoplace == true then
								local structureCframe = Clientstructure.PrimaryPart.CFrame
								placedstructure = PlaceSctructure:InvokeServer(Clientstructure.Name, structureCframe)
								
								if placedstructure == true then
									placingStructure = false
									Clientstructure:Destroy()
									StructureFrame.Visible = true
								end
							end
						end
					end
				end)
			end
		end)
	end
end



Something is clearly broken here with your PrimaryPart, what do you have the PrimaryPart set to? The position of it and the PrimaryPart itself is what is broken. Is the PrimaryPart unanchored or colliding through the map and being voided? Is the PrimaryPart even set on said model?

What lines are no 49 and 57? chars chars

Set the PrimaryPart in the model if it hasn’t been done so already.

As for the "Attempt to index nil with “Position”, there is no property “Position” for whatever you are trying to find the position of.

Yes, the PrimaryPart for each model is set to a specific Primary part for each model, each part is anchored. There are no visual gameplay issues but when RenderStepped is running the output after the first placement goes crazy, I’ll include a video.

Here’s the video link to what is happening:

I was able to solve the issue by creating checks for each function if the client structure was == nil then break the function and disconnect the render service.

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