Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script still runs even when tool is unequip?

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

0

try writing for it to stop when it is unequipped

0

I've been try that but I'm not sure how

0

I've tried disabling the code, Cloning and destroying and It doesn't work

1 answer

local equipped = false
local tool = script.Parent

tool.Equipped:Connect(function()
	equipped = true
	
end)

tool.Unequipped:Connect(function()
    equipped = false
end)

if equipped == true then
	--write your code here
end

if equipped == false then
	--write your code here
end

5 comments

0

@TheKatYTArmy Thanks

0

Wait it didn't seem to work

0

did the output say anything?

0

or you can just remove the if equipped == false

0

I did try that but for some reason the script only works when the code is in the equipped == false but when I remove one of them it won't work

This discussion is preserved as it was. New answers, comments, and votes are closed.