Does anyone know why this building script to place pieces and build results in errors?
It results in an error with position, repeated no matter what and the second error if I rotated it before hand.
Errors at Line 85 and 93.
85: Attempt to index nil with position
93: Model:SetPrimaryCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.
The errors only happen after it runs once. It's like it needs to update and realize that stuff doesn't exist. I don't really know.
Also there is another script, but it works fine.
001 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
002 | local placeStructure = replicatedStorage:WaitForChild( "PlaceStructure" ) |
003 | local structures = replicatedStorage:WaitForChild( "Structures" ) |
004 | local sillhouettes = replicatedStorage:WaitForChild( "Sillhouettes" ) |
006 | local UIS = game:GetService( "UserInputService" ) |
007 | local RunService = game:GetService( "RunService" ) |
010 | local player = game:GetService( "Players" ).LocalPlayer |
011 | local StructureFrame = script.Parent |
012 | local char = player.Character or player.Character:Wait() |
013 | local HumanoidRootPart = char:WaitForChild( "HumanoidRootPart" ) |
014 | local mouse = player:GetMouse() |
015 | local yBuildingOffset = 0.5 |
016 | local maxPlacingDistance = 50 |
017 | local rKeyIsPressed = false |
018 | local placingStructure = false |
020 | local goodTOPlace = false |
024 | local rotating = false |
027 | function handleKeyInputStarted(input) |
028 | if rotating = = false then |
032 | local rotationSpeed = 90 |
033 | while rKeyIsPressed do |
035 | if placingStructure = = true then |
036 | yOrientation = yOrientation + rotationSpeed |
039 | until rKeyIsPressed = = false |
047 | function handleKeyInputEnded(input) |
048 | if input.KeyCode = = Enum.KeyCode.R then |
049 | rKeyIsPressed = false |
054 | function handleMouseInputBegan(input) |
055 | if placingStructure = = true then |
056 | if goodTOPlace = = true then |
057 | local StructureCFrame = clientStructure.PrimaryPart.CFrame |
058 | placedStructure = placeStructure:InvokeServer(clientStructure.Name, StructureCFrame) |
060 | if placedStructure = = true then |
061 | placingStructure = false |
062 | clientStructure:Destroy() |
063 | StructureFrame.Enabled = true |
070 | function handleInputStarted(input) |
071 | if input.KeyCode = = Enum.KeyCode.R then |
072 | handleKeyInputStarted(input) |
073 | elseif input.UserInputType = = Enum.UserInputType.MouseButton 1 then |
074 | handleMouseInputBegan(input) |
079 | function handleRenderStepped() |
080 | local mouseRay = mouse.UnitRay |
081 | local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000 ) |
082 | local ignoreList = { clientStructure, char } |
083 | local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList) |
085 | if hit and (HumanoidRootPart.Position - clientStructure.PrimaryPart.Position).Magnitude < maxPlacingDistance then |
091 | local newAnglesCFrame = CFrame.Angles( 0 , math.rad(yOrientation), 0 ) |
092 | local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.z) |
093 | clientStructure:SetPrimaryPartCFrame(newCFrame * newAnglesCFrame) |
097 | function handleBrickButtonPressed(structureName) |
098 | StructureFrame.Enabled = false |
100 | if placingStructure = = false then |
101 | placingStructure = true |
103 | clientStructure = sillhouettes:FindFirstChild(structureName):Clone() |
104 | for x, v in pairs (clientStructure:GetChildren()) do |
105 | if v:IsA( "Part" ) then |
109 | clientStructure.Parent = game.Workspace |
111 | local startingCFrame = CFrame.new( 0 , - 2 , - 15 ) |
112 | clientStructure:SetPrimaryPartCFrame(HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)) |
114 | RunService.RenderStepped:Connect(handleRenderStepped) |
119 | for _, structureButton in pairs (StructureFrame.PurchasesScreen:GetChildren()) do |
120 | if structureButton:IsA( "TextButton" ) then |
121 | structureButton.MouseButton 1 Up:Connect( function () |
122 | handleBrickButtonPressed(structureButton.Name) |
128 | UIS.InputBegan:Connect(handleInputStarted) |
129 | UIS.InputEnded:Connect(handleKeyInputEnded) |
I know this is a really long script and I'm sorry I just didn't know where else to look. If you read this all and find an answer then I really don't know how to thank you.