so im making an isometric zombie game and my gun system (the one does all the raycast and stuff) works fine, if you have no ammo, the gun gets disabled. but that feature is really bugging me, for the fx manager, the one that does all the animations, even if your at 0 ammo, and the gun is disabled, you still play the animations. i looked through dev forum but if you find anything that could help please refrence it in the comments
weapon system:
local weapon = script.Parent
local rp = game:GetService("ReplicatedStorage")
local _settings = require(weapon.Settings)
local canShoot = _settings.canShoot
local rayPoint = weapon:WaitForChild("RayPoint")
local ammoEvent = rp.Remotes:WaitForChild("AmmoEvent")
local gunFireRate = _settings.gunFireRate
local gunDamage = _settings.gunDamage
local ammoCapacity = _settings.gunAmmoCapacity
local clipCapacity = _settings.gunClipCapacity
local currentAmmo = _settings.currentAmmo
local currentAmmoCapacity = ammoCapacity
local gunGUI = weapon.UIScript.GunGUI
local gunGUIText = gunGUI.AmmoFrame.AmmoTextLabel
currentAmmo = clipCapacity
canShoot = true
weapon.Activated:Connect(function()
print("shot")
if canShoot then
wait()
canShoot = false
print("ammo capicity"..clipCapacity)
local rayOrigin = rayPoint.CFrame.Position
local rayDirection = rayPoint.CFrame.LookVector
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {weapon}
rayParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * _settings.gunRayDistance, rayParams)
wait(0.05)
if currentAmmoCapacity > 0 then
ammoFunc()
canShoot = false
else
canShoot = true
end
--print("currentAmmoCap"..currentAmmoCapacity)
--print(currentAmmo)
--print(clipCapacity)
--print(ammoCapacity)
if raycastResult then
local part = raycastResult.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(gunDamage)
end
end
wait(gunFireRate)
canShoot = true
end
end)
function ammoFunc()
currentAmmo -= 1
local ammo = currentAmmo
-- Fire the RemoteEvent to the client
ammoEvent:FireAllClients(currentAmmo)
if currentAmmoCapacity ~= 0 and currentAmmo == 0 then
currentAmmoCapacity -= clipCapacity
currentAmmo = clipCapacity
print(currentAmmoCapacity)
end
end
fx manager
local weapon = script.Parent
local rp = game:GetService("ReplicatedStorage")
local _settings = require(weapon.Settings)
local canShoot = _settings.canShoot
local alive = true
local equipped = false
local equipAnim = script:FindFirstChild("WeaponEquip")
local idleAnim = script:FindFirstChild("WeaponIdle")
local unequipAnim = script:FindFirstChild("WeaponUnequip")
local shootAnim = script:FindFirstChild("WeaponShoot")
local weaponClone = rp.Guns:WaitForChild("Pistol") -- change this to the weapon model
local weaponParticles = weapon:WaitForChild("GunParticle")
local ShotSound = weapon:WaitForChild("ShotSound")
local EquipSound = weapon:WaitForChild("EquipSound")
local gunFireRate = _settings.gunFireRate
canShoot = true
weapon.Equipped:Connect(function()
local hum = script.Parent.Parent.Humanoid
equipped = true
EquipSound:Play()
-- anim tracks
local equipAnimTrack = hum:LoadAnimation(equipAnim)
local idleAnimTrack = hum:LoadAnimation(idleAnim)
-- anim playing
if equipped == true then
equipAnimTrack:Play()
idleAnimTrack:Play()
else
equipAnimTrack:Stop()
idleAnimTrack:Stop()
end
end)
weapon.Unequipped:Connect(function()
equipped = false
EquipSound:Play()
local clone = weaponClone:Clone()
clone.Parent = workspace
local part = clone:WaitForChild("WeldHandle")
local weld = Instance.new("Weld",part)
local plr = script:FindFirstAncestorOfClass("Player")
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
-- anim tracks
local unequipAnimTrack = hum:LoadAnimation(unequipAnim)
weld.Part0 = part --Attach to the handle
weld.Part1 = char["Right Arm"] --Attach to the right hand.
weld.C0 = CFrame.new(0, 0, 0) + Vector3.new(0,char["Right Arm"].Size.Y/2,0)
-- anim playing
unequipAnimTrack:Play()
wait(unequipAnimTrack.Length + 0.1)
clone:Destroy()
for i,v in pairs(plr.Character.Humanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
end)
weapon.Activated:Connect(function()
local hum = script.Parent.Parent.Humanoid
if canShoot then
print("Can Shoot: ".. tostring(canShoot))
canShoot = false
-- particles
local muzzle = weaponParticles:WaitForChild("MuzzleFlash")
local sparkle = weaponParticles:WaitForChild("Sparkle")
local smoke = weaponParticles:WaitForChild("Smoke")
-- anim tracks
local shootAnimTrack = hum:LoadAnimation(shootAnim)
shootAnimTrack:Play()
ShotSound:Play()
muzzle.Enabled = true
sparkle.Enabled = true
smoke.Enabled = true
wait(0.05) -- change as needed
muzzle.Enabled = false
sparkle.Enabled = false
smoke.Enabled = false
wait(gunFireRate)
canShoot = true
end
end)
settings (module script)
local Settings =
{
gunDamage = 20; -- gun damage lolol
gunAmmoCapacity = 5; -- how much bullets you can use
gunClipCapacity = 5; -- how much bullets you can use before reloading
gunFireRate = 1; -- firerate
gunRayDistance = 500; -- how much in studs does the bullet reach
currentAmmo = 0; -- curent ammo
canShoot = true;
}
return Settings
any pictures or videos feel free to ask in the comments