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

how to make weapon not attack when unequipped?

Asked by 4 years ago

I made a fireball script that when you equip it, you can press q to throw it. The only problem is that you can do it while it is unequipped also. I tried making an if statement for checking if it is equipped first, but it did nothing. I then tried a while loop but it crashes the studio.

here is the code right now.

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
local replicatedStorage = game:GetService("ReplicatedStorage")
local fireAnim = replicatedStorage.FireballAnimation
local debounce = false
local equipped = false
script.Parent.Equipped:Connect(function()
    equipped = true
    if equipped == true do -- check if it is equipped
        UserInputService.InputBegan:Connect(function(Input , IsTyping)
        if not IsTyping then
            if not debounce then

                debounce = true

                if Input.KeyCode == Enum.KeyCode.Q then

                    game.ReplicatedStorage.Fireball:FireServer(fireAnim)
                    wait(1.5)

                end

                wait(0.1)
                debounce = false
                end
            end 
        end)
    end

end)
script.Parent.Unequipped:Connect(function()
    print("Unequipped")
    equipped = false -- make it unequipped
end)

any help?

0
the while loop probably crashed because you didnt put a wait() Necro_las 412 — 4y

1 answer

Log in to vote
1
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 years ago

I think that once you equip the ball, you are connecting InputService, and it stays connected even after you unequip. A better way to usa a tool is using Activated event.

local cooldown = 2
local canCast = true

script.Parent.Activated:Connect(function()
    if canCast then
        canCast = false
        delay(cooldown, function() canCast = true end) -- this will run after 2 sec
        game.ReplicatedStorage.Fireball:FireServer(fireAnim)
    end
end
0
Thank you so much! Idoskii 4 — 4y
Ad

Answer this question