I’m trying to make a simple gun like this:
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: