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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I gotcha, you wanna implement a delete and pick up mechanic using a hammer tool. Sounds like a solid plan. I'll guide you through the process, and I'll assume you have a basic understanding of Roblox scripting (Lua). If you need to share scripts, feel free to paste them, but I'll provide general examples that you can adapt to your codebase.
Step 1: Create the Hammer Tool
In Roblox Studio:
Create a new Tool in ServerScriptService (right-click > Insert Object > Tool). Name it "Hammer".
Move the Hammer tool to StarterPack. This will equip the tool to every player when they join the game, in the first slot (as you desired).
Add a Script (not LocalScript) inside the Hammer tool and name it "HammerScript". We'll use this script to handle the logic.
Step 2: HammerScript (Tool Script)
Paste this code into the HammerScript:
lua
-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
-- Config
local DELETE_OPTION = "Delete"
local PICKUP_OPTION = "Pick Up"
-- Variables
local player = Players.LocalPlayer -- won't work in a normal Script, but we will fix this later
local tool = script.Parent
local highlightedWorker = nil
local uiPrompts = {
Delete = nil,
PickUp = nil,
}
-- Functions
local function createUIButtons(worker)
-- Create UI buttons (we'll use a ScreenGui)
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "HammerOptions"
screenGui.Parent = player.PlayerGui
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0, 200, 0, 100)
frame.Position = UDim2.new(0.5, -100, 0.5, -50)
frame.BackgroundTransparency = 0.5
frame.BackgroundColor3 = Color3.new(0.2, 0.2, 0.2)
frame.Parent = screenGui
-- Delete Button
local deleteButton = Instance.new("TextButton")
deleteButton.Size = UDim2.new(0.45, 0, 0.45, 0)
deleteButton.Position = UDim2.new(0.05, 0, 0.05, 0)
deleteButton.Text = DELETE_OPTION
deleteButton.Parent = frame
uiPrompts.Delete = deleteButton
-- Pick Up Button
local pickUpButton = Instance.new("TextButton")
pickUpButton.Size = UDim2.new(0.45, 0, 0.45, 0)
pickUpButton.Position = UDim2.new(0.5, 0, 0.05, 0)
pickUpButton.Text = PICKUP_OPTION
pickUpButton.Parent = frame
uiPrompts.PickUp = pickUpButton
-- Highlight the worker
local highlight = Instance.new("Highlight")
highlight.FillTransparency = 0.5
highlight.OutlineTransparency = 0
highlight.OutlineColor = Color3.new(1, 1, 0) -- Yellow
highlight.Parent = worker
highlightedWorker = worker
end
local function destroyUIButtons()
if player.PlayerGui:FindFirstChild("HammerOptions") then
player.PlayerGui.HammerOptions:Destroy()
end
if highlightedWorker then
local highlight = highlightedWorker:FindFirstChild("Highlight")
if highlight then
highlight:Destroy()
end
highlightedWorker = nil
end
end
-- Connections
tool.Equipped:Connect(function()
-- Ready to listen for clicks
end)
tool.Unequipped:Connect(function()
-- Clean up UI when tool is unequipped
destroyUIButtons()
end)
-- We'll handle mouse clicks using UserInputService
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and tool.Parent == player.Character then
-- Raycast from mouse position to detect workers
local mouse = player:GetMouse()
local target = mouse.Target
if target and target:FindFirstChild("WorkerConfig") then -- assuming you have a WorkerConfig inside your worker models
createUIButtons(target)
-- Now, listen for button clicks
uiPrompts.Delete.MouseButton1Click:Connect(function()
-- **DELETE WORKER LOGIC HERE**
print("Deleting worker:", target.Name)
destroyUIButtons()
end)
uiPrompts.PickUp.MouseButton1Click:Connect(function()
-- **PICK UP WORKER LOGIC HERE**
print("Picking up worker:", target.Name)
destroyUIButtons()
end)
else
destroyUIButtons() -- Clicked on something else, hide UI
end
end
end)
This script creates the UI buttons and highlight when you click on a worker with the hammer equipped. You'll need to fill in the DELETE WORKER LOGIC and PICK UP WORKER LOGIC sections.
The Issue with LocalPlayer
Since this script runs on the server (Tool Script), Players.LocalPlayer won't work. We need to get the player wielding the tool another way.
Fix: Move the HammerScript to a LocalScript inside the Hammer tool. This will make Players.LocalPlayer work as expected, since LocalScripts run on the client.
Just right-click the HammerScript > Convert to > LocalScript.
Your Tasks:
Replace DELETE WORKER LOGIC with your actual delete function (e.g., call a RemoteEvent to notify the server to delete the worker from the saved data and destroy the model).
Replace PICK UP WORKER LOGIC with your pick-up function (e.g., add the worker model to the player's inventory, update saved data, etc.). This might also require RemoteEvents to sync with the server.
Ensure your worker models have a WorkerConfig (or any other unique identifier) so the script can detect them.
RemoteEvents for Server Communication
For both delete and pick-up actions, you'll likely need to inform the server to:
Delete the worker from saved data and destroy the model (for delete).
Update the player's inventory and saved data (for pick-up).
Here's a quick example:
Create a RemoteEvent in ReplicatedStorage named WorkerManagerEvent.
In your DELETE WORKER LOGIC, fire the event:
lua
local workerManagerEvent = game.ReplicatedStorage.WorkerManagerEvent
workerManagerEvent:FireServer("DeleteWorker", target) -- target is the worker model
In a Script (e.g., ServerScriptService > WorkerManagerScript), listen for the event:
lua
workerManagerEvent.OnServerEvent:Connect(function(player, action, workerModel)
if action == "DeleteWorker" then
-- Delete worker from saved data
-- Destroy the worker model
workerModel:Destroy()
elseif action == "PickUpWorker" then
-- Handle pick-up logic here (update player's inventory, saved data)
end
end)
Do the same for PICK UP WORKER LOGIC, firing WorkerManagerEvent with "PickUpWorker" as the action.
That's the basic flow! You might need to tweak this to fit your existing saving/loading system, but this should give you a solid starting point.
Test it out, and if you hit any snags or have questions, share your scripts, and I'll help you debug! Happy developing!