-
What do you want to achieve? This is a bit complicated, but my goal is to make a tool with a table that holds a set of grabbable objects (parts, and if possible, models and unions too), and if you hold down your mouse while it’s hovering over one of the objects in the table, it gets an outline around it, and you’re able to move it around freely until you drop it, in which the outline will disappear and it will fall back to the ground.
-
What is the issue? I’m not an incredible coder, and I have no clue how to make basically any of the mechanics I need.
-
What solutions have you tried so far? I’ve looked at as many posts as I can to find a way to do this, but the closest I could find was a guide on how to make it so that you can grab unanchored parts and move them around.
This is a pretty complicated system, so if anyone could help me with this, I would greatly appreciate it. Thanks.
1 Like
This is definitely a challenging project, but here is an approach you can take to achieve your goal:
- Create a tool that the player can equip. You can use a Script or LocalScript to handle the functionality of the tool.
- Create a table that holds the grabbable objects you want to include. You can populate this table manually or programmatically by searching for objects with a specific tag or name.
- Use a raycast to detect when the player is hovering over one of the grabbable objects. When the mouse is hovering over an object, you can highlight it by adding an outline effect or changing its color.
- When the player clicks and holds down the mouse button while hovering over a grabbable object, you can create a new part or weld the object to a temporary part to make it a child of the player’s character. This will allow the player to move the object around freely.
- While the player is holding the object, you can update the position of the object to match the position of the player’s mouse. You can use the mouse’s RaycastResult.Position property to get the current position of the mouse in the game world.
- When the player releases the mouse button, you can release the object by either removing the temporary part or un-welding the object from the temporary part. You can also remove any highlighting or outline effect on the object.
Here is some sample code to get you started:
local tool = script.Parent
local grabbableObjects = {} -- Populate this table with the grabbable objects
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local selectedObject = nil
local selectedOffset = Vector3.new()
local function highlightObject(object)
-- Add highlight effect to object
end
local function unhighlightObject(object)
-- Remove highlight effect from object
end
local function grabObject(object)
selectedObject = object
selectedOffset = (mouse.Hit.Position - selectedObject.Position)
selectedObject.Anchored = true
end
local function releaseObject()
if selectedObject ~= nil then
selectedObject.Anchored = false
selectedObject = nil
end
end
mouse.Move:Connect(function()
if selectedObject ~= nil then
local newPosition = mouse.Hit.Position - selectedOffset
selectedObject.CFrame = CFrame.new(newPosition)
else
-- Use raycast to detect if mouse is hovering over a grabbable object
local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 100)
local hit, position, normal = workspace:FindPartOnRay(ray, nil, false, true)
if hit ~= nil and grabbableObjects[hit.Name] then
if selectedObject == nil then
highlightObject(hit)
end
if mouse.Button1Down then
grabObject(hit)
end
else
if selectedObject == nil then
unhighlightObject(hit)
end
end
end
end)
mouse.Button1Up:Connect(function()
releaseObject()
end)
This is just a starting point, and you will likely need to modify and expand on this code to fit your specific needs. Good luck with your project buddy!