Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does anyone know why this building script to place pieces and build results in errors?

Asked by 3 years ago

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.

local replicatedStorage = game:GetService("ReplicatedStorage")
local placeStructure = replicatedStorage:WaitForChild("PlaceStructure")
local structures = replicatedStorage:WaitForChild("Structures")
local sillhouettes = replicatedStorage:WaitForChild("Sillhouettes")

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

--local player = game.Player.LocalPlayer
local player = game:GetService("Players").LocalPlayer
local StructureFrame = script.Parent
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 0.5
local maxPlacingDistance = 50
local rKeyIsPressed = false
local placingStructure = false
local yOrientation=0
local goodTOPlace = false
local placedStructure
local clientStructure

local rotating = false

-- Handles start of temporary brick rotation
function handleKeyInputStarted(input)
    if rotating == false then
        rotating = true
        rKeyIsPressed = true

        local rotationSpeed = 90
        while rKeyIsPressed do
            wait()
            if placingStructure == true then
                yOrientation = yOrientation + rotationSpeed         
                repeat
                    wait()
                until rKeyIsPressed == false
            end
        end
        rotating = false
    end
end

-- Handles end of temporary brick rotation
function handleKeyInputEnded(input)
    if input.KeyCode == Enum.KeyCode.R then
        rKeyIsPressed = false                       
    end
end

-- Handles construction of actual Brick when user is satisfied with placement/rotation 
function handleMouseInputBegan(input)
    if placingStructure ==  true then
        if goodTOPlace == true then
            local StructureCFrame = clientStructure.PrimaryPart.CFrame
            placedStructure = placeStructure:InvokeServer(clientStructure.Name, StructureCFrame)

            if placedStructure == true then
                placingStructure = false
                clientStructure:Destroy()
                StructureFrame.Enabled = true
            end
        end
    end
end

-- Handles general input (mouse or keyboard) input
function handleInputStarted(input)
    if input.KeyCode == Enum.KeyCode.R then
        handleKeyInputStarted(input)
    elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
        handleMouseInputBegan(input)
    end
end

-- Handles rendering the temporary Brick during placement
function handleRenderStepped()
    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 (HumanoidRootPart.Position - clientStructure.PrimaryPart.Position).Magnitude < maxPlacingDistance then
        goodTOPlace = true
    else
        goodTOPlace = false
    end

    local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
    local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.z)
    clientStructure:SetPrimaryPartCFrame(newCFrame * newAnglesCFrame)
end

-- Handles constructing the temporary placement brick when the create button is pressed.
function handleBrickButtonPressed(structureName)
    StructureFrame.Enabled = false

    if placingStructure == false then
        placingStructure = true

        clientStructure = sillhouettes:FindFirstChild(structureName):Clone()
        for x, v in pairs(clientStructure:GetChildren()) do
            if v:IsA("Part") then
                v.CanCollide = false
            end
        end
        clientStructure.Parent = game.Workspace

        local startingCFrame = CFrame.new(0, -2, -15)
        clientStructure:SetPrimaryPartCFrame(HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame))

        RunService.RenderStepped:Connect(handleRenderStepped)
    end
end

-- Iterate over all Buttons in the StructureFrame and attach a mouse listener
for _, structureButton in pairs(StructureFrame.PurchasesScreen:GetChildren()) do
    if structureButton:IsA("TextButton") then
        structureButton.MouseButton1Up:Connect(function() 
            handleBrickButtonPressed(structureButton.Name)  
        end)
    end
end


UIS.InputBegan:Connect(handleInputStarted)
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.

Answer this question