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

How to fix/simplify this Placement System Script? [closed]

Asked by 6 years ago

Me and my friend have worked together on this placement system script for a while now as a school project.

I need to make this code shorter and more efficient/clean.

The whole code is here:

wait()
local rs = game:WaitForChild("ReplicatedStorage")
local base = game.Workspace.GrassTop
local placeablePart = rs:WaitForChild("PlaceableObjects")
local Menu = game.Players.LocalPlayer.PlayerGui.PlacementSystem.ObjectMenu.ObjectList
local CurrentlyPlacing = false
local rotation = 0
local canPlace = false

local first = true

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")

function roundAToB(a,b)
    return(math.floor((a/b)+0.5)*b)
end

local modelPreview, modelPlace

modelRotate = mouse.KeyDown:connect(function(key)
    if (key == "r") then
        rotation = rotation + 90
    end
end)

---------- OBJECTS ----------

Menu.Wall.MouseButton1Click:connect(function()
    script.SelectedPart.Value = placeablePart.Wall
    CurrentlyPlacing = true
    cloneModel = script.SelectedPart.Value:clone()
    cloneModel.Parent = game.Workspace
end)

-----------------------------

count = 0
modelPreview = runService.RenderStepped:connect(function()
    if CurrentlyPlacing == true then
        local modelCFrame = cloneModel:GetModelCFrame()
        mouse.TargetFilter = cloneModel
        local hit = mouse.Hit.p
        cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X,4),1.5,roundAToB(hit.Z,4)) * CFrame.Angles(0, math.rad(rotation), 0))        
        if (cloneModel.PrimaryPart.Position.X > base.Position.X-base.Size.X/2) and (cloneModel.PrimaryPart.Position.Z > base.Position.Z-base.Size.Z/2) and (cloneModel.PrimaryPart.Position.X < base.Position.X+base.Size.X/2) and (cloneModel.PrimaryPart.Position.Z < base.Position.Z+base.Size.Z/2) then     
            canPlace = true         
            cloneModel:Destroy()
            cloneModel = script.SelectedPart.Value:clone()
            cloneModel.Parent = game.Workspace
            cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X,4),1.5,roundAToB(hit.Z,4)) * CFrame.Angles(0, math.rad(rotation), 0))
        -- error is somewhere around here       
            for i,v in pairs(cloneModel:GetChildren()) do
                if v:IsA('BasePart') then
                    v.Transparency = 0.7
                    v.CanCollide = false
                end
            end
        else
            canPlace = false
            for i,v in pairs(cloneModel:GetChildren()) do
                if v:IsA('BasePart') then
                    v.Transparency = 1
                    v.CanCollide = false
                end
            end
        end
    else
        if not first then
            cloneModel:Destroy()
        end
    end
end)

modelPlace = mouse.Button1Down:connect(function()
    if CurrentlyPlacing == true then
        if canPlace == true then
            local new = cloneModel:clone()
            new.Parent = workspace.Tycoons.TycoonTemplate.PurchasedObjects
            for i,v in pairs(new:GetChildren()) do
                if v:IsA('BasePart') then
                    v.Transparency = 0
                    v.CanCollide = true
    --          Specific part properties below:
                elseif v.Name == "Arrow" then
                    v.CanCollide = false
                    v.Transparency = 0.7
                elseif v.Name == "Drop" then
                    v.CanCollide = false
                    v.Transparency = 0.5
                end
            end
        end
        cloneModel:Destroy()
        script.CurrentlyPlacing.Value = false
        canPlace = false
    end
end)
1
Is there anything actually broken in this code? I'm happy to look over it for you but it's gonna be on the backburner if there are no actual bugs to fix. adark 5487 — 6y
0
Since there is no indication that this code isn't functioning, i'm going to moderate. +1 to omit rep loss. Goulstem 8144 — 6y

Closed as Primarily Opinion-Based by adark and Goulstem

This question has been closed because it is a discussion about a topic focused on diverse opinions, which isn't a good fit for our Q&A format.

Why was this question closed?