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

Why Does My Script Runs Even When My Tool Is Unequipped?

When I don't equip it and press q, It's still cloning the sword model into my Character.

local Tool = script.Parent.Parent
local IS = script.Parent.IsSheathed
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local torso = char.Torso
local mouse = player:GetMouse()
local ButtonPressed = false

Tool.Equipped:connect(function()
mouse.KeyDown:connect(function(key)
    if key == "q" then
	if not ButtonPressed then
	ButtonPressed = true
	print("x")
	if IS.Value == false then
		print("y")
	script.Parent.IA.Disabled = true
	script.Parent.AttackAnim.Disabled = true
	--script.Parent.IAnim.Disabled = true
	script.Parent.Parent.Damage.Disabled = true
	script.Parent.Parent.Handle.Name = "hand"
	script.Parent.Parent.SwordModel.Transparency = 1
	
	Back = script.Parent.Parent.SwordModel:clone()
	Back.Transparency = 0
	Back.Parent =  char
	
	local weld = Instance.new("Weld")
	weld.Name = "BackWeld"
	weld.Part0 = torso
	weld.Part1 = Back
	weld.C0 = CFrame.new(0,0,0.6)
	weld.C0 = weld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(180),math.rad(0),44.5)
	weld.Parent = Back
	
	IS.Value = true
	wait(1)
	ButtonPressed = false
	elseif IS.Value == true then
		print("z")
	script.Parent.IA.Disabled = false
	script.Parent.AttackAnim.Disabled = false
	--script.Parent.IAnim.Disabled = false
	script.Parent.Parent.Damage.Disabled = false
	script.Parent.Parent.hand.Name = "Handle"
	script.Parent.Parent.SwordModel.Transparency = 0
	x = char:FindFirstChild("SwordModel")
	if x then
		x:Destroy()
	end
	IS.Value = false
	wait(1)
	ButtonPressed = false
	end
	end
	end
end)
end)

3 comments

0

isn't keydown deprecated?

0

You can replicate with: function Activated() the code end Tool.Activated:Connect(Activated)

0

Just replicated "ButtonPressed" for the Activated()

1 answer

✓ Accepted answer

Because the mouseEvent gets 'activated' and it will keep working, best idea is to make a variable that you can use as a sort of debounce like this

local Tool = script.Parent.Parent
local IS = script.Parent.IsSheathed
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local torso = char.Torso
local mouse = player:GetMouse()
local ButtonPressed = false

Equipped = false

Tool.Equipped:connect(function()
Equipped = true
mouse.KeyDown:connect(function(key)
    if key == "q" and Equipped then
    if not ButtonPressed then
    ButtonPressed = true
    print("x")
    if IS.Value == false then
        print("y")
    script.Parent.IA.Disabled = true
    script.Parent.AttackAnim.Disabled = true
    --script.Parent.IAnim.Disabled = true
    script.Parent.Parent.Damage.Disabled = true
    script.Parent.Parent.Handle.Name = "hand"
    script.Parent.Parent.SwordModel.Transparency = 1

    Back = script.Parent.Parent.SwordModel:clone()
    Back.Transparency = 0
    Back.Parent =  char

    local weld = Instance.new("Weld")
    weld.Name = "BackWeld"
    weld.Part0 = torso
    weld.Part1 = Back
    weld.C0 = CFrame.new(0,0,0.6)
    weld.C0 = weld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(180),math.rad(0),44.5)
    weld.Parent = Back

    IS.Value = true
    wait(1)
    ButtonPressed = false
    elseif IS.Value == true then
        print("z")
    script.Parent.IA.Disabled = false
    script.Parent.AttackAnim.Disabled = false
    --script.Parent.IAnim.Disabled = false
    script.Parent.Parent.Damage.Disabled = false
    script.Parent.Parent.hand.Name = "Handle"
    script.Parent.Parent.SwordModel.Transparency = 0
    x = char:FindFirstChild("SwordModel")
    if x then
        x:Destroy()
    end
    IS.Value = false
    wait(1)
    ButtonPressed = false
    end
    end
    end
end)
end)

Tool.Unequipped:Connect(function()
	Equipped = false
end)

2 comments

0

Thanks

0

Np :)

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