Skip to content
The Chat Archive
mistral-medium-2505· Mistral AI LMArena Human Preference 140K

sup dude. I am making a roblox game right now. I have my placing and saving system…

  1. User

    sup dude. I am making a roblox game right now. I have my placing and saving system working nicely but now i wanna add a delete and pick up tool. So i want it so on join, a player will load with a hammer tool that is always in their first slot. Then when they take it out and click on a placed worker, 2 ui prompts show up and a highlight appears around the worker. 1 ui button is for picking up and 1 is for deleting, if you need to see any kind of my scripts let me know.

  2. Assistant

    Roblox Hammer Tool with Delete and Pickup Functionality

    Hey dude! Sounds like you're building something cool. I'll help you create a hammer tool system with delete and pickup functionality. Here's how you can implement it:

    1. First, let's create the hammer tool

    -- ServerScriptService/HammerTool.lua
    local Tool = script.Parent
    local Players = game:GetService("Players")
    
    local function onEquipped(tool, player)
        -- You can add any equipped effects here
    end
    
    local function onUnequipped(tool, player)
        -- Clean up any effects when unequipped
    end
    
    Tool.Equipped:Connect(onEquipped)
    Tool.Unequipped:Connect(onUnequipped)
    

    2. Create the UI prompts

    -- StarterGui/HammerUI.lua
    local Players = game:GetService("Players")
    local Player = Players.LocalPlayer
    local PlayerGui = Player:WaitForChild("PlayerGui")
    
    local HammerUI = Instance.new("ScreenGui")
    HammerUI.Name = "HammerUI"
    HammerUI.Parent = PlayerGui
    
    -- Create the UI elements (initially hidden)
    local deleteButton = Instance.new("TextButton")
    deleteButton.Name = "DeleteButton"
    deleteButton.Text = "DELETE"
    deleteButton.Size = UDim2.new(0, 200, 0, 50)
    deleteButton.Position = UDim2.new(0.5, -100, 0.7, 0)
    deleteButton.Visible = false
    deleteButton.Parent = HammerUI
    
    local pickupButton = Instance.new("TextButton")
    pickupButton.Name = "PickupButton"
    pickupButton.Text = "PICKUP"
    pickupButton.Size = UDim2.new(0, 200, 0, 50)
    pickupButton.Position = UDim2.new(0.5, -100, 0.8, 0)
    pickupButton.Visible = false
    pickupButton.Parent = HammerUI
    

    3. Create the highlight effect

    -- ReplicatedStorage/HighlightModule.lua
    local HighlightModule = {}
    
    function HighlightModule.createHighlight(part)
        local highlight = Instance.new("Highlight")
        highlight.FillColor = Color3.new(0, 1, 0) -- Green color
        highlight.OutlineColor = Color3.new(0, 0.5, 0)
        highlight.Parent = part
        return highlight
    end
    
    function HighlightModule.removeHighlight(part)
        for _, child in ipairs(part:GetChildren()) do
            if child:IsA("Highlight") then
                child:Destroy()
            end
        end
    end
    
    return HighlightModule
    

    4. Main tool functionality

    -- Tool script (inside the hammer tool)
    local Tool = script.Parent
    local Players = game:GetService("Players")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local HighlightModule = require(ReplicatedStorage:WaitForChild("HighlightModule"))
    
    local player = nil
    local currentHighlight = nil
    local currentTarget = nil
    
    -- UI references
    local PlayerGui = nil
    local deleteButton = nil
    local pickupButton = nil
    
    local function showUI(show)
        if PlayerGui then
            deleteButton.Visible = show
            pickupButton.Visible = show
        end
    end
    
    local function onActivated()
        if not currentTarget then return end
    
        -- Raycast to check what we're clicking
        local character = player.Character
        if not character then return end
    
        local humanoid = character:FindFirstChild("Humanoid")
        if not humanoid then return end
    
        local rootPart = character:FindFirstChild("HumanoidRootPart")
        if not rootPart then return end
    
        -- Simple raycast (you might want to improve this)
        local rayOrigin = rootPart.Position
        local rayDirection = (currentTarget.Position - rayOrigin).Unit * 20
        local raycastParams = RaycastParams.new()
        raycastParams.FilterDescendantsInstances = {character}
        raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    
        local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
        if raycastResult and raycastResult.Instance:IsDescendantOf(currentTarget) then
            -- Show UI
            showUI(true)
        end
    end
    
    local function onUnequipped()
        showUI(false)
        if currentHighlight then
            HighlightModule.removeHighlight(currentTarget)
            currentHighlight = nil
            currentTarget = nil
        end
    end
    
    local function onEquipped(mouse)
        player = Players:GetPlayerFromCharacter(Tool.Parent)
        if not player then return end
    
        -- Get UI references
        PlayerGui = player:WaitForChild("PlayerGui")
        local hammerUI = PlayerGui:WaitForChild("HammerUI")
        deleteButton = hammerUI:WaitForChild("DeleteButton")
        pickupButton = hammerUI:WaitForChild("PickupButton")
    
        -- Connect button events
        deleteButton.MouseButton1Click:Connect(function()
            if currentTarget then
                -- Call server to delete the worker
                game.ReplicatedStorage.DeleteWorker:FireServer(currentTarget)
                showUI(false)
                HighlightModule.removeHighlight(currentTarget)
                currentHighlight = nil
                currentTarget = nil
            end
        end)
    
        pickupButton.MouseButton1Click:Connect(function()
            if currentTarget then
                -- Call server to pickup the worker
                game.ReplicatedStorage.PickupWorker:FireServer(currentTarget)
                showUI(false)
                HighlightModule.removeHighlight(currentTarget)
                currentHighlight = nil
                currentTarget = nil
            end
        end)
    
        -- Connect mouse target events
        mouse.TargetFilter = function(part)
            -- Check if the part is a worker (you'll need to adjust this)
            if part:FindFirstAncestor("Worker") then
                return true
            end
            return false
        end
    
        mouse.TargetChanged:Connect(function(newTarget)
            -- Remove previous highlight
            if currentHighlight then
                HighlightModule.removeHighlight(currentTarget)
                currentHighlight = nil
                currentTarget = nil
            end
    
            -- Add new highlight if valid target
            if newTarget and newTarget:FindFirstAncestor("Worker") then
                currentTarget = newTarget:FindFirstAncestor("Worker")
                currentHighlight = HighlightModule.createHighlight(currentTarget)
            else
                showUI(false)
            end
        end)
    end
    
    Tool.Equipped:Connect(onEquipped)
    Tool.Unequipped:Connect(onUnequipped)
    Tool.Activated:Connect(onActivated)
    

    5. Server-side handling

    -- ServerScriptService/WorkerHandler.lua
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    
    -- Create remote events
    local deleteWorkerEvent = Instance.new("RemoteEvent")
    deleteWorkerEvent.Name = "DeleteWorker"
    deleteWorkerEvent.Parent = ReplicatedStorage
    
    local pickupWorkerEvent = Instance.new("RemoteEvent")
    pickupWorkerEvent.Name = "PickupWorker"
    pickupWorkerEvent.Parent = ReplicatedStorage
    
    local function onDeleteWorker(player, worker)
        -- Verify the worker belongs to the player
        if worker and worker:FindFirstChild("Owner") and worker.Owner.Value == player then
            worker:Destroy()
        end
    end
    
    local function onPickupWorker(player, worker)
        -- Verify the worker belongs to the player
        if worker and worker:FindFirstChild("Owner") and worker.Owner.Value == player then
            -- Implement your pickup logic here
            -- For example, you might want to:
            -- 1. Remove the worker from the workspace
            -- 2. Add it back to the player's inventory
            -- 3. Update any data stores
    
            worker:Destroy()
        end
    end
    
    deleteWorkerEvent.OnServerEvent:Connect(onDeleteWorker)
    pickupWorkerEvent.OnServerEvent:Connect(onPickupWorker)
    

    6. Giving the tool to players on join

    -- ServerScriptService/PlayerSetup.lua
    local Players = game:GetService("Players")
    local ServerStorage = game:GetService("ServerStorage")
    
    local function onPlayerAdded(player)
        player:WaitForChild("Backpack")
    
        -- Clone the hammer tool from ServerStorage
        local hammerTool = ServerStorage:WaitForChild("HammerTool"):Clone()
        hammerTool.Parent = player.Backpack
    
        -- Make sure it's always in the first slot
        player.CharacterAdded:Connect(function(character)
            local humanoid = character:WaitForChild("Humanoid")
            humanoid:EquipTool(hammerTool)
        end)
    end
    
    Players.PlayerAdded:Connect(onPlayerAdded)
    

    Implementation Notes:

    1. Make sure you have:

      • A HammerTool in ServerStorage
      • The UI setup in StarterGui
      • The HighlightModule in ReplicatedStorage
      • The remote events created on the server
    2. You'll need to adjust the worker detection logic to match how your workers are structured in the game.

    3. The pickup functionality will depend on how your inventory system works - you'll need to implement that part based on your specific needs.

    4. For better raycasting, you might want to use the mouse's Hit position instead of a simple raycast from the humanoid root part.

    Would you like me to explain any part in more detail or adjust any of the functionality to better match your game's specific needs?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.