-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragObject.lua
More file actions
60 lines (49 loc) · 1.71 KB
/
DragObject.lua
File metadata and controls
60 lines (49 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
--//Put this under any sort of part as a script
--//Make sure to also include a BoolValue instance as a child named "Dragged" along with a drag detector
local part = script.Parent
local drag = part.PlayerDrag
local partAttachment = Instance.new("Attachment")
partAttachment.Parent = part
local alignPosition = Instance.new("AlignPosition")
alignPosition.Attachment0 = partAttachment
alignPosition.Responsiveness = 100
alignPosition.Parent = part
local RunService = game:GetService("RunService")
local connection
drag.DragStart:Connect(function(player)
local character = player.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local stringvalue = Instance.new("StringValue")
stringvalue.Parent = part
stringvalue.Value = player.Name
part.Dragged.Value = true
if character and humanoid and humanoid.Health ~= 0 then
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
local hrpAttachment = hrp:FindFirstChild("RootAttachment") or hrp:FindFirstChildWhichIsA("Attachment")
alignPosition.Attachment1 = hrpAttachment
alignPosition.Enabled = true
connection = RunService.Heartbeat:Connect(function()
if (part.Position - hrp.Position).Magnitude >= 15 then
drag.Enabled = false
if connection then
connection:Disconnect()
connection = nil
end
elseif (part.Position - hrp.Position).Magnitude <= 15 then
drag.Enabled = true
end
wait(0.5)
drag.Enabled = true
end)
end
end
end)
drag.DragEnd:Connect(function(player)
alignPosition.Attachment1 = nil
alignPosition.Enabled = false
if part:FindFirstChildWhichIsA("StringValue") then
part:FindFirstChildWhichIsA("StringValue"):Destroy()
end
part.Dragged.Value = false
end)