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

how to make weapon not attack when unequipped?

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?

1 comment

0

the while loop probably crashed because you didnt put a wait()

1 answer

✓ Accepted answer

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

1 comment

0

Thank you so much!

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