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

Had a question about my "Placables" system breaking another script when a new item is placed?

Asked by 1 year ago

PlacablesClient: Located inside of the "HammerToolGui" basic rundown is that this script waits for a player to click a button in the gui the corresponds to what item there trying to place. The script gets the model (and its parts) and creates a clone to provide the player with a visual on where there newly placed item will be. The script looks for the mouse position and other important stuff like that and sends it off to the server as soon as the player releases their left mouse button. The server receives this info and places the actual item accordingly. There's also a rotate function inside of this script but I don't think that's causing my problem so i wont go into detail. (I know its poorly written but I'm brand new)

local PlaceEvent = game.ReplicatedStorage.PlaceableEvents.PlaceEvent
local ObjectFolder = game.ReplicatedStorage:WaitForChild("PlaceableStorage")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidrootPart = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()

local Frame = script.Parent.ItemList
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local GUI = game.StarterGui.HammerToolGui.MainFrame

local PlacingObject = false
local RotatingObject = false
local maxPlacingDistance = 50


for i, Button in pairs (Frame:GetChildren()) do

    if Button:IsA("TextButton") then

        Button.MouseButton1Click:Connect(function()

            local goodToPlace = false

            if PlacingObject == false then 

                PlacingObject = true
                GUI.Visible = false

                local RotationAmount = 0

                local PreviewObject = ObjectFolder:FindFirstChild(Button.Name):Clone()
                PreviewObject.Parent = game.Workspace.PlaceableThings


                for i, Parts in pairs(PreviewObject:GetDescendants()) do

                    if Parts:IsA("BasePart") then

                        Parts.Transparency = 0.5
                        Parts.CanCollide = false

                        RunService.RenderStepped:Connect(function()
                            local mouseRay = Mouse.UnitRay
                            local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
                            local ignoreList = {PreviewObject}
                            local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

                            if hit and (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidrootPart.Position - Parts.Position) .Magnitude < maxPlacingDistance then 
                                goodToPlace = true
                                Parts.BrickColor = BrickColor.new("Forest green")
                            else
                                goodToPlace = false
                                Parts.BrickColor = BrickColor.new("Crimson")
                            end
                        end)
                    end
                end


                    UIS.InputBegan:Connect(function(Key, GameProcessed)

                        if not GameProcessed then

                            if Key.KeyCode == Enum.KeyCode.R then

                                RotatingObject = true

                                while RotatingObject == true do 
                                    wait()
                                    RotationAmount +=2
                                end
                            end
                        end
                    end)

                    UIS.InputEnded:Connect(function(Key)

                        if Key.KeyCode == Enum.KeyCode.R then

                            RotatingObject = false
                        end
                    end)

                    RunService.RenderStepped:Connect(function()

                        if PlacingObject == true then 

                            Mouse.TargetFilter = PreviewObject

                            if PreviewObject:FindFirstChild("MainPart") then 

                                local ObjectCFrame = CFrame.new(Mouse.Hit.Position.X, Mouse.Hit.Position.Y + PreviewObject.PrimaryPart.Size.Y / 2, Mouse.Hit.Position.Z)
                                local ObjectAngles = CFrame.Angles(0, math.rad(RotationAmount), 0)

                                PreviewObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)
                            end
                        end
                    end)

                    Mouse.Button1Up:Connect(function()

                        if PlacingObject == true then
                            if goodToPlace == true then

                                PlacingObject = false

                                for i, x in pairs(Player.PlayerGui.InventoryGui.Frame.Inventory.Items.Grid:GetChildren()) do
                                    if x:IsA("Frame") and x.Name == Button.Name then
                                        x:Destroy()
                                        break
                                    end
                                end

                                PlaceEvent:FireServer(PreviewObject.Name, PreviewObject.PrimaryPart.CFrame)

                                GUI.Visible = true
                                PreviewObject:Destroy()


                        end
                    end
                end)
            end
        end)
    end
end

PickupManager: Located inside of "StarterPlayerScripts" basic rundown this script works in conjunction with another system (completely separate from placables system) my inventory system. The script waits for a player to get close enough to the item on the ground and then displays a gui based on that. The script then listens for the player to press the G key to "collect" the item.

local function onInputEnded(input, mouse, player, pickupItemEvent)
    if input.KeyCode == Enum.KeyCode.G then
        local target = mouse.Target
        if target and target:FindFirstChild("Pickable") then
            local item = target
            local distanceFromItem = player:DistanceFromCharacter(item.Position)
            if distanceFromItem < 7 then
                print(item.Name)
                print(item.Desc.Value)
                pickupItemEvent:FireServer(item.Name, item.Desc.Value)
                print("fired to server")
                item:Destroy()
            end
        end
    end
end

local function onInputChanged(input, mouse, pickupInfoGui)
    local target = mouse.Target
    if target and target:FindFirstChild("Pickable") then
        local item = target
        pickupInfoGui.Adornee = item
        pickupInfoGui.ObjectName.Text = item.Name
        pickupInfoGui.Enabled = true
    else
        pickupInfoGui.Adornee = nil
        pickupInfoGui.Enabled = false
    end
end

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickupItem = ReplicatedStorage.InvEvents.PickUpClient

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local PlayerGui = player:WaitForChild("PlayerGui")
local PickupInfoGui = PlayerGui:WaitForChild("PickUpInfoGui")

UIS.InputChanged:Connect(function(input)
    onInputChanged(input, mouse, PickupInfoGui)
end)

UIS.InputEnded:Connect(function(input)
    onInputEnded(input, mouse, player, PickupItem)
end)

So now that you have all of that information I can tell you about the actual problem. When I join the game and place one of the "placables" the game will no longer allow me to pick up items, the gui to pick things up wont even display (keep in mind these to scripts do not work in conjunction with each other in any way). What's strange is if the player dies they can pick up items again but again after a "placable" is placed the player can no longer pick up items.

Anyone have any ideas?

0
This is dizzy land :O T3_MasterGamer 2189 — 1y
0
and your name is MasterGamer? I'm just asking a question, if you don't have an answer keep your mouth shut ;) KCbiulder 0 — 1y

Answer this question