Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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)
0
isn't keydown deprecated? LordOfWatermelons 27 — 6y
0
You can replicate with: function Activated() **the code** end Tool.Activated:Connect(Activated) KCC_Mega 0 — 2y
0
Just replicated "ButtonPressed" for the Activated() KCC_Mega 0 — 2y

1 answer

Log in to vote
0
Answered by 6 years ago

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)

0
Thanks FlonexVorry 28 — 6y
0
Np :) User#20388 0 — 6y
Ad

Answer this question