I recently read through a dev forum tutorial about making an object dragging system similar to Lumber Tycoon 2’s. The post is here if you want to read it. The tutorial was solid and I learned a lot but I ran into an issue that I’m not sure if anyone else experiences. For some reason when I try and drag parts in my game, it doesn’t move for the first 10-20 seconds. After I wait a bit the parts finally start to move when I try and drag them and I have no idea what is causing this issue. If someone could explain this for me that would be great. Also, I ran this in a blank game so I’ve come to the conclusion that it isn’t lag from an abundance of scripts running that is causing this problem.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService") --we will use this service for something later
local target
local down
mouse.Button1Down:connect(function()
if mouse.Target ~= nil and mouse.Target.Locked == false and mouse.Target == workspace.Pizza.Part then
target = mouse.Target
mouse.TargetFilter = target
down = true
local gyro = Instance.new("BodyGyro") --adding the forces
gyro.Name = "Gyro"
gyro.Parent = target
gyro.MaxTorque = Vector3.new(500000, 500000, 500000)
local force = Instance.new("BodyPosition")
force.Name = "Force"
force.Parent = target
force.MaxForce = Vector3.new(10000, 10000, 10000) --you may wanna modify this a bit, since it effect if you can move an object or wrong depending on its weight/mass (in other words, the force might not be strong enough)
else
print("No Pizza To Move")
end
end)
game:GetService("RunService").RenderStepped:Connect(function() --replaced the move event with renderstepped because it works better in some cases, renderstepped is an event that fires every frame, basically super fast, look it up it is important!
if down == true and target ~= nil then
target.Gyro.CFrame = target.CFrame --this is to keep rotation
target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 20)
end
end)
mouse.Button1Up:connect(function()
if target then --of course we wanna remove the forces after the dragging, first check if there was even a target
if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then --check if there was a force
target.Gyro:Destroy() --DESTROY!!!!
target.Force:Destroy()
end
end
down = false
mouse.TargetFilter = nil
target = nil
end)