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

How to fix model cloning multiple times?

Asked by 4 years ago

Every time i cancel placement model start cloning multiple times.

local Placement = {}
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Placement.MaxPlacingDistance = 300
Placement.CanStart = true
Placement.CanPlace = true
Placement.IsPlacing = false
Placement.CanRotate = true
Placement.CanCancel = true
Placement.Placed = false
Placement.PlacedName = "Unknown"
local PreviewModel
local Rotation = 0
local Target = nil
local Pos = nil
local Norm = nil
function SnapPositions()
    if PreviewModel then
        local RayIgnoreList = {Player.Character, PreviewModel}
        local MouseRay = Mouse.UnitRay
        local NewRay = Ray.new(MouseRay.Origin, MouseRay.Direction.unit * Placement.MaxPlacingDistance)
        Target, Pos, Norm = workspace:FindPartOnRayWithIgnoreList(NewRay, RayIgnoreList)
    end
end
function MoveModel()
    if PreviewModel then
        SnapPositions()
        EndPosX = Pos.X + (Norm.X * (PreviewModel.PrimaryPart.Size.X * 0.5))
        EndPosY = Pos.Y + (Norm.Y * (PreviewModel.PrimaryPart.Size.Y * 0.5))
        EndPosZ = Pos.Z + (Norm.Z * (PreviewModel.PrimaryPart.Size.Z * 0.5))
        PreviewModel:SetPrimaryPartCFrame(PreviewModel.PrimaryPart.CFrame:Lerp(CFrame.new(EndPosX, EndPosY, EndPosZ) * CFrame.Angles(0, math.rad(Rotation), 0), 0.2))
    end
end
function RotateModel(Input, Other)
    if Input.KeyCode == Enum.KeyCode.Q then
        if PreviewModel then
            Rotation = Rotation + 45
        end
    end
end
function PlaceModel()
    if Placement.IsPlacing and Placement.CanPlace then
        script.PlaceModelEvent:FireServer(EndPosX, EndPosY, EndPosZ, Rotation, PreviewModel.Name)
        Placement.Placed = true
        Placement.PlacedName = PreviewModel.Name
        wait(0.01)
        Placement.Placed = false
        Placement.PlacedName = "Unknown"
        wait(0.05)
    end
end
function Placement.CancelPlacementFromScript()
    Placement.IsPlacing = false
    Placement.CanPlace = true
    Placement.CanStart = true
    Placement.CanRotate = true
    PreviewModel:Destroy()
    PreviewModel = nil
end
function CancelPlacement(Input, Other)
    if Input.KeyCode == Enum.KeyCode.X then
            if Placement.CanCancel and Placement.IsPlacing then
                Placement.IsPlacing = false
                Placement.CanPlace = true
                Placement.CanStart = true
                Placement.CanRotate = true
                PreviewModel:Destroy()
                PreviewModel = nil
            end
      end
end
function Placement.StartPlacing(Model)
    if Placement.CanStart and not Placement.IsPlacing and not PreviewModel then
        Placement.CanPlace = true
        Placement.CanStart = false
        Placement.IsPlacing = true
        PreviewModel = Model:Clone()
        PreviewModel.Parent = workspace
        local PreviewModelChildren = PreviewModel:GetChildren()
        for i, v in pairs(PreviewModelChildren) do
            if v:IsA("BasePart") then
                v.CanCollide = false
                v.Anchored = true
                v.Transparency = 0.55
            end
        end
        UserInputService.InputBegan:Connect(RotateModel)
        UserInputService.InputBegan:Connect(CancelPlacement)
        Mouse.Button1Down:Connect(PlaceModel)
        while wait(0) and Placement.IsPlacing == true do
            MoveModel()
        end
    end
end
return Placement

When i cancel placing and start placing again multiple models appear. Error: https://youtu.be/wqk2KcA4EOQ

0
your connecting Mouse.ButtonDown to PlaceModel every single time you run the script mybituploads 304 — 4y

Answer this question