0% found this document useful (0 votes)
80 views3 pages

Roblox Drag and Grab Script

The document is a Lua script for a Roblox game that implements a grabbing mechanism for objects using the mouse. It allows players to grab, drag, and drop objects with a 'Grab' BoolValue, and triggers an explosion upon dropping if certain conditions are met. Additionally, it includes functionality to adjust the drag distance using the mouse wheel and manages the creation of a visual drag indicator (DragBall).

Uploaded by

aleee.ang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Roblox Drag and Grab Script

The document is a Lua script for a Roblox game that implements a grabbing mechanism for objects using the mouse. It allows players to grab, drag, and drop objects with a 'Grab' BoolValue, and triggers an explosion upon dropping if certain conditions are met. Additionally, it includes functionality to adjust the drag distance using the mouse wheel and manages the creation of a visual drag indicator (DragBall).

Uploaded by

aleee.ang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

-- grab script

local CAS = game:GetService("ContextActionService")

local GrabObject = nil


local GrabStart = false
local Dragger = nil
local isDragging = false -- New variable to track if an object is being dragged
local dragDistance = 10 -- Default drag distance from the player's head

local player = [Link]


local character = [Link] or [Link]:Wait()
local mouse = player:GetMouse()
local camera = [Link]

function Grab(actionName, UserInputState, InputObject)


if actionName == "Grab" then
if UserInputState == [Link] then
-- Start the grab if not already dragging
if not isDragging then
local Magnitude = ([Link] -
[Link]).magnitude
if Magnitude < 10 then
if [Link] then
-- Check if the target part has a BoolValue named "Grab"
local targetGrabValue = [Link]:FindFirstChild("Grab")

if targetGrabValue and targetGrabValue:IsA("BoolValue") and


[Link] == true then
GrabObject = [Link]
GrabStart = true
isDragging = true -- Set dragging state to true

local DragBall = createDragBall()


[Link] = [Link]
Dragger = DragBall

[Link] = GrabObject

local DragBallWeld = weldBetween(DragBall, GrabObject)


addMover(DragBall)

while Dragger do
-- Create a ray from the user's head to the mouse.
local cf = [Link]([Link], [Link])
[Link] = (cf + ([Link] *
dragDistance)).Position
[Link] = [Link] *
[Link]([Link].X, [Link].Y,
[Link].Z)
wait()
end
[Link] = nil
else
-- Optionally, you can give feedback if the part doesn't have the
"Grab" BoolValue.
print("This part cannot be grabbed because it does not have the
'Grab' BoolValue or it is set to false.")
end
end
end
else
-- Stop the grab if already dragging
if GrabObject then
-- Trigger explosion logic on drop
triggerExplosionOnDrop(GrabObject)
GrabObject = nil
end
GrabStart = false
if Dragger then
Dragger:Destroy()
Dragger = nil
end
isDragging = false -- Set dragging state to false
end
end
end
end

function weldBetween(a, b)
local weld = [Link]("ManualWeld", a)
weld.C0 = [Link]:inverse() * [Link]
weld.Part0 = a
weld.Part1 = b
return weld
end

function addMover(part)
local newMover = [Link]("BodyPosition")
[Link] = part
[Link] = [Link](40000, 40000, 40000)
newMover.P = 40000
newMover.D = 1000
[Link] = [Link]
[Link] = "Mover"

local newRot = [Link]("BodyGyro")


[Link] = part
[Link] = [Link](3000, 3000, 3000)
newRot.P = 3000
newRot.D = 500
[Link] = [Link]
[Link] = "RotMover"

local RotOffset = [Link]("CFrameValue")


[Link] = "RotOffset"
[Link] = part
end

function createDragBall()
local DragBall = [Link]("Part")
[Link] = [Link]
[Link] = [Link]
[Link] = [Link](.2, .2, .2)
[Link] = "Block"
[Link] = "DragBall"
[Link] = workspace
local particles = [Link].p:Clone()
[Link] = DragBall
return DragBall
end

-- Function to adjust drag distance with mouse wheel


function onScrollWheel(input)
if isDragging then
if [Link] == [Link] then
dragDistance = dragDistance + [Link].Z
dragDistance = [Link](dragDistance, 2, 20) -- Clamp the drag distance to
a range
end
end
end

-- Function to trigger explosion after dropping


function triggerExplosionOnDrop(object)
local exp = object:FindFirstChild("exp")
local time = object:FindFirstChild("time")

if exp and exp:IsA("BoolValue") and [Link] == true then


if time and time:IsA("NumberValue") then
local explosionTime = [Link]
spawn(function()
wait(explosionTime)
-- Create explosion only if the object still exists
if [Link] then
-- Create explosion
local explosion = [Link]("Explosion")
[Link] = [Link]
[Link] = 10
[Link] = 50000
[Link] = workspace

-- Destroy the object


object:Destroy()
print("Object exploded after being dropped!")
end
end)
else
warn("Object has 'exp' but is missing a valid 'time' NumberValue.")
end
end
end

-- Connect the scroll wheel input


game:GetService("UserInputService").InputChanged:Connect(onScrollWheel)

CAS:BindAction("Grab", Grab, false, [Link].MouseButton1)

You might also like