MouseButtonDown/Tool.Equipped or Unequipped not working

I’m trying to make a simple gun like this:
image

However, MouseButtonDown, Tool.Equipped, and Tool.Unequipped are not working. They are not being detected.

Heres my code: (Local script)

local tool = script.Parent

local config = require(tool.Config)

local player = game.Players.LocalPlayer

local character = player.CharacterAdded:Wait() or player.Character
local humanoid: Humanoid = character:WaitForChild("Humanoid")

local mouse = player:GetMouse()

local isFiring = false
local isReloading = false

local equipped = false

local ammo = config.maxAmmo
local spareAmmo = config.spareAmmo

mouse.Button1Down:Connect(function()
	if isReloading == false and isFiring == false and equipped and ammo > 0 then
		ammo -= 1
		isFiring = true
		tool.Fire:FireServer(mouse.Hit.p, tool)
		--local fireTrack = character:WaitForChild("Animator"):LoadAnimation(tool.Fire)
		--fireTrack:Play()
	end
end)

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

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

function reload()
	if spareAmmo > 0 and equipped == true then
		isReloading = true
		spareAmmo -= 1
		--local reloadTrack = character:WaitForChild("Animator"):LoadAnimation(tool.Reload)
		--reloadTrack:Play()
		task.wait(2)
		ammo = 1
	end
end

(Server Script):

local tool = script.Parent

local config = require(tool.Config)

tool.Fire.OnServerEvent:Connect(function(plr: Player, hitPos, tool)
	local animator = Instance.new("Animator", plr.Character)
	game:GetService("Debris"):AddItem(animator, 2)
	local sound = Instance.new("Sound", tool.Handle)
	sound.SoundId = config.fireSound
	sound:Play()
	if hitPos then
		if hitPos.Parent:FindFirstChild("Humanoid") then
			local humanoid = hitPos.Parent:FindFirstChild("Humanoid")
			if humanoid.Parent.Name ~= plr.Character.Name then
				humanoid.Health -= config.damage
			end
		end
	end
	sound.Stopped:Connect(function()
		sound:Destroy()
	end)
end)

(Module Script… Config)

local config = {
	maxAmmo = 1;
	spareAmmo = 24;
	
	damage = 20;
	boatDamage = 50;
	
	reloadAnimation = 'rbxassetid://0';
	fireAnimation = 'rbxassetid://0';
	
	reloadSound = 'rbxassetid://0';
	fireSound = 'rbxassetid://85758437775155';
	equipSound = 'rbxassetid://0';
	unequipSound = 'rbxassetid://0';
}

return config

And the weapon:
image

1 Like

Note, I haven’t fully scripted reload yet because I’m making sure this works first…

Is the script throwing any errors?

None at all, when I put ‘print(’‘)’ inbetween lines, the only one that outputted was the one outside of the functions…

1 Like

image
is RequiresHandle and Enabled properties enabled?

Yes they are. (Sorry for late response I pretty much gave up on this for a while).

SOLUTION: I put ‘Print()’ inbetween my variables, which is something I would not think to do usually. But I did and it only printed “1” meaning in this, it was waiting for my character to load even tho it was, so I will change that.

local tool = script.Parent

local config = require(tool.Config)

local player = game.Players.LocalPlayer

print('1')
local character = player.CharacterAdded:Wait() or player.Character
local humanoid: Humanoid = character:WaitForChild("Humanoid")
print('2')

local mouse = player:GetMouse()
print('3')

local isFiring = false
local isReloading = false
print('4')

local equipped = false

local ammo = config.maxAmmo
local spareAmmo = config.spareAmmo
print('5')

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.