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

Looking for help on converting my existing placeable system to work with models?

Asked by 2 years ago

Hello, I was looking for help on converting my existing placeable system to work with models. Currently it only works with parts but I have been trying for the past couple of days to figure out a some what simple way to incorporate models. Here is the code, I added a few labels to make reading easier digest:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")

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

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.StructureFrame 
local char = player.Character or player.Character:Wait()
local HumanoidrootPart = char:WaitForChild("HumanoidRootPart")

local mouse = player:GetMouse()

local yBuildOffset = 5
local maxPlacingDistance = 50
local rKeyIsPressed = false
local placingStructure = false

for _, structureButton in pairs(StructureFrame:GetChildren()) do 
    if structureButton:IsA("TextButton") then
        structureButton.MouseButton1Up:Connect(function()

            StructureFrame.Visible = false

            local yOrientation = 0
            local goodToPlace = false
            local placedStructure 

            if placingStructure == false then 
                placingStructure = true
------------------------------------------------------Problem Area----------------------------------------------------

                local clientStructure = Structures:FindFirstChild(structureButton.Name):Clone() -- Grabs the part that is matched to the name of text button in GUI
                --------------------Fancy Looks so you can see what your placing--------------------
                clientStructure.BrickColor = BrickColor.new ("Forest green")
                clientStructure.Material = "Neon"
                clientStructure.CanCollide = false
                clientStructure.Parent = game.Workspace
                ------------------------------------------------------------------------------------
                local startingCFrame = CFrame.new(0, -2, -15)
                clientStructure.CFrame = HumanoidrootPart.CFrame:ToWorldSpace(startingCFrame)

                RunService.RenderStepped:Connect(function()
                    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 (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidrootPart.Position - clientStructure.Position) .Magnitude < maxPlacingDistance then --decides if plcemnt area is good or bad
                        goodToPlace = true
                        clientStructure.BrickColor = BrickColor.new("Forest green") --Keeps color green if in valid placment area
                    else
                        goodToPlace = false
                        clientStructure.BrickColor = BrickColor.new("Crimson") --Changes color red if not in valid placment area
                    end

                    local newAnglesCframe = CFrame.Angles(0, math.rad(yOrientation), 0)
                    local newCFrame = CFrame.new(position.X, position.Y + yBuildOffset, position.Z)
                    clientStructure.CFrame = newCFrame * newAnglesCframe
                end)

                UIS.InputBegan:Connect(function(input)
                    if input.KeyCode == Enum.KeyCode.R then
                        rKeyIsPressed = true

                        local rotationSpeed = 5
                        while rKeyIsPressed do 
                            wait()
                            if placingStructure == true then 
                                yOrientation = yOrientation +rotationSpeed

                            end
                        end
                    end
                end)

                UIS.InputEnded:Connect(function(input)
                    if input.KeyCode == Enum.KeyCode.R then 
                        rKeyIsPressed = false
                    end
                end)
                ---------------Accessing and placing object (object placement handled elsewhere)--------------
                UIS.InputBegan:Connect(function(input)
                    if input.UserInputType == Enum.UserInputType.MouseButton1 then
                        if placingStructure == true then 
                            if goodToPlace == true then
                                local SructureCFrame = clientStructure.CFrame
                                placedStructure = PlaceStructure:InvokeServer(clientStructure.Name, SructureCFrame)

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

Answer this question