I'm trying to make a tool where you can grab stuff but I still manage to grab things even when the tool is unequip (Note: I'm new to coding so please dumb everything down)
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
local tool = script.Parent
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
local tool = script.Parent
tool.Equipped:Connect(function()
if not script.Parent.Enabled then
return
end
mouse.Button1Down:connect(function()
if mouse.Target ~= nil and mouse.Target.Locked == false 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)
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)
end)
tool.Unequipped:Connect(function()
end)

3 comments
try writing for it to stop when it is unequipped
I've been try that but I'm not sure how
I've tried disabling the code, Cloning and destroying and It doesn't work