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

Why is the script not finding the attachment?

Asked by 1 year ago

I have created a piece off code that allows the players to put anchors down which connect together and the last one placed connects to the player by a rope. However, the attachments are randomly not being found after the delete has run a phew times.

There is a part in ServerStorage called Anchor which is cloned. Parented to it are the following things:

  • A attachment, also called Anchor.

  • Two variables, nextanchor and previousanchor.

I have added an if statement to prevent the error breaking it in the remove, but it won't work correctly if it skips the part in the if statement.

The line off code is similar in place and remove, and it is this line that is having a error in both: NewRope.Attachment1 = Anchor.Anchor

The full script:

game.ReplicatedStorage.Remotes.PlaceAnchor.OnServerEvent:Connect(function(Player)
    local Character = Player.Character
    local LastAnchor:ObjectValue = Player:FindFirstChild("LastAnchor")
    local Anchors:IntValue = Player:FindFirstChild("Anchors")
    local Rope:RopeConstraint = Character:FindFirstChild("Rope")

    if Anchors.Value >= 1 then
        local Anchor = game.ServerStorage.Anchor:Clone()
        Anchor.Parent = workspace
        Anchor.CFrame = Character.HumanoidRootPart.CFrame
        if LastAnchor.Value then
            local LastAnchorAttachment = LastAnchor.Value.Anchor
            local NewRope = Instance.new("RopeConstraint")
            NewRope.Parent = LastAnchor.Value
            NewRope.Attachment0 = LastAnchorAttachment
            NewRope.Attachment1 = Anchor.Anchor
            NewRope.Length = 52
            NewRope.Visible = true
            NewRope.Name = "Rope"
            Anchor.PreviosAnchor.Value = LastAnchor.Value
            LastAnchor.Value.NextAnchor.Value = Anchor
        end
        Anchors.Value -= 1
        LastAnchor.Value = Anchor
        Rope.Attachment1 = Anchor.Anchor
        Rope.Visible = true
    end
end)

game.ReplicatedStorage.Remotes.RemoveAnchor.OnServerEvent:Connect(function(Player)
    local Character = Player.Character
    local LastAnchor:ObjectValue = Player:FindFirstChild("LastAnchor")
    local Anchors:IntValue = Player:FindFirstChild("Anchors")
    local Rope:RopeConstraint = Character:FindFirstChild("Rope")
    local OP = OverlapParams.new()
    OP.FilterDescendantsInstances = {Character}
    local SurroundingParts = workspace:GetPartBoundsInBox(Character.HumanoidRootPart.CFrame, Vector3.new(10,10,10),OP)
    for i, SurroundingPart in pairs(SurroundingParts) do
        if SurroundingPart.Name == "Anchor" and SurroundingPart:IsA("Part") then
            if SurroundingPart.PreviosAnchor.Value then
                local LastAnchor2 = SurroundingPart.PreviosAnchor.Value
                if SurroundingPart.NextAnchor.Value then
                    local LastRope:RopeConstraint = LastAnchor2:FindFirstChild("Rope")
                    local NextAnchor = SurroundingPart.NextAnchor.Value
                    if LastAnchor2:FindFirstChild("Anchor") then
                        LastRope.Attachment1 = NextAnchor.Anchor
                    end
                    SurroundingPart:Destroy()
                    Anchors.Value += 1
                    -- Next anchor placed
                    break
                else
                    -- Next anchor not placed
                    local NextAnchor = Character
                    local LastRope:RopeConstraint = Character:FindFirstChild("Rope")
                    if LastAnchor2:FindFirstChild("Anchor") then
                        LastRope.Attachment1 = LastAnchor2.Anchor
                    end
                    SurroundingPart:Destroy()
                    Anchors.Value += 1
                    break
                end
            else
                -- No anchor before.
                if SurroundingPart.NextAnchor.Value then
                    SurroundingPart:Destroy()
                    Anchors.Value += 1
                    -- Next anchor placed
                    break
                else
                    -- Next anchor not placed
                    local NextAnchor = Character
                    SurroundingPart:Destroy()
                    Anchors.Value += 1
                    break
                end
            end
        end
    end
end)

Answer this question