- What do you want to achieve?
I’m trying to make a draggable part (called “4D”) by adding two “arrow” parts (pointing away from each other) next to it. The parts are grouped in a model (with a stationary part called “3D” as well) and both the arrows have a click detector in them. I’m using the code further down in the post to make them draggable, where the scripts are inside each of the click detectors and the local script is inside StarterPlayerScripts.
- What is the issue?
When I try dragging the arrows, sometimes they jump way further than the intended 1 stud, so that the mouse cursor lands on the other arrow, and sometimes, the distance printed (h) is 1 or -1 even though the part named 4D and the part named 3D share the same position.
- What solutions have you tried so far?
When the parts were moving too far at once, I added another condition to an if statement to check if the mouse cursor was over 1 stud away from the part and not only over 1 stud away from the last position. That fixed some of the issue, at least when I only had one draggable arrow, but when I tried making the second arrow draggable too, the problems really started. Since it’s a very specific script, I didn’t really know what to search on the DevForum, so I haven’t really looked for a solution.
- Code:
Script:
local cd = script.Parent
local rs = game:GetService("ReplicatedStorage")
local hoverEvent = rs:FindFirstChild("hoverEvent") or Instance.new("RemoteEvent")
hoverEvent.Parent = rs
hoverEvent.Name = "hoverEvent"
cd.MouseHoverEnter:Connect(function(player)
hoverEvent:FireClient(player, script.Parent.Parent, true)
end)
cd.MouseHoverLeave:Connect(function(player)
hoverEvent:FireClient(player, script.Parent.Parent, false)
end)
LocalScript:
local cd = script.Parent
local rs = game:GetService("ReplicatedStorage")
local hoverEvent = rs:WaitForChild("hoverEvent")
local player = game.Players.LocalPlayer
local hover = false
local clicked = false
hoverEvent.OnClientEvent:Connect(function(part, h)
clicked = false
hover = h
local mouse = player:GetMouse()
local pos1
local pos2
local R = part.Parent:FindFirstChild("4D").Size.X/2
mouse.Button1Down:Connect(function()
clicked = true
if hover == true then
pos1 = mouse.Hit.Position
mouse.Move:Connect(function()
if hover == true and clicked == true then
pos2 = mouse.Hit.Position
if (pos2 - pos1).X > 1 and (pos2 - part.Position).X > 1 then
pos1 = mouse.Hit.Position
for i, brick in pairs(part.Parent:GetChildren()) do
if brick.Name ~= "3D" then
brick.Position = Vector3.new(brick.Position.X+1,brick.Position.Y,brick.Position.Z)
else
local h = (brick.Position - brick.Parent:FindFirstChild("4D").Position).X
print(h)
local r = math.sqrt(R^2-h^2)
brick.Size = Vector3.new(2*r,2*r,2*r)
end
end
elseif (pos2 - pos1).X < -1 and (pos2 - part.Position).X < -1 then
pos1 = mouse.Hit.Position
for i, brick in pairs(part.Parent:GetChildren()) do
if brick.Name ~= "3D" then
brick.Position = Vector3.new(brick.Position.X-1,brick.Position.Y,brick.Position.Z)
else
local h = (brick.Position - brick.Parent:FindFirstChild("4D").Position).X
print(h)
local r = math.sqrt(R^2-h^2)
brick.Size = Vector3.new(2*r,2*r,2*r)
end
end
end
end
end)
end
mouse.Button1Up:Connect(function()
clicked = false
end)
end)
end)