so i’ve tried making ammo for a gun.
when the ammo runs out the reloading wont start and the pointlight isnt turned off
i tried to move the part where it checks if theres enough ammo but it won’t work or will break the gun
heres the code, also theres no client side for the ammo only server
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Tool:Tool = script.Parent
local Handle = Tool:FindFirstChild("Handle")
local ShootPart = script.Parent.main:FindFirstChild("Attachment")
local RemoteEvent = Tool:FindFirstChild("RemoteEvent")
local gui = script.Parent.Parent.Parent.PlayerGui:WaitForChild("AmmoGui")
local ammocount = gui:WaitForChild("AmmoCount")
local guiname = gui:WaitForChild("gunname")
local OnCooldown = false
local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=13742702578"
local track
local Do = {
Damage = 15;
Cooldown = 0.3;
Visualize = true;
ShootSound = "rbxassetid://2811598570";
}
local ammo = 30
local maxammo = ammo
local reloading = false
local function reload()
if reloading then return end
reloading = true
tool.Handle.reload:Play()
wait(4.5)
tool.flash.Enabled = true
print("Reloaded")
ammo = maxammo
reloading = false
end
RemoteEvent.OnServerEvent:Connect(function(Player,Received)
if not OnCooldown and ammo > 0 and not reloading then
OnCooldown = true
task.delay(Do.Cooldown,function()
OnCooldown = false
end)
local Origin = ShootPart.WorldPosition
local Direction = (Received.Position-Origin).Unit*3000
local Raycast = workspace:Raycast(Origin,Direction)
local Intersection = Raycast and Raycast.Position or Origin + Direction
local Distance = (Origin - Intersection).Magnitude
local Visualizer = Instance.new("Part")
Visualizer.CanTouch = false
Visualizer.CanCollide = false
Visualizer.CanQuery = false
Visualizer.CastShadow = false
Visualizer.Anchored = true
Visualizer.Material = Enum.Material.Metal
Visualizer.Color = Color3.fromRGB(255, 255, 127)
Visualizer.Size = Vector3.new(.15,.15,Distance)
Visualizer.CFrame = CFrame.new(Origin, Intersection)*CFrame.new(0,0,-Distance/2)
if Do.Visualize == true then
Visualizer.Parent = workspace
TweenService:Create(Visualizer,TweenInfo.new(.3),{Transparency = 1, Size = Vector3.new(0,0,Distance)}):Play()
Debris:AddItem(Visualizer,.35)
ammo = ammo -1
elseif ammo < 1 then
print("reload")
tool.Handle.noammo:Play()
tool.flash.Enabled = false
tool.main.PointLight.Enabled = false
reload()
end
if (Do.ShootSound ~= "" or Do.ShootSound ~= nil) then
local Sound = Instance.new("Sound")
Sound.SoundId = Do.ShootSound
Sound.Volume = .35
Sound.PlayOnRemove = true
Sound.Parent = Handle
Sound:Destroy()
end
script.Parent.Equipped:Connect(function()
gui.Enabled = true
guiname.Text = tool.FullName.Value
track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action
track.Looped = true
track:Play()
ammocount.Text = ammo.. "/" ..maxammo
end)
script.Parent.Unequipped:Connect(function()
guiname.Text = " "
track:Stop()
gui.Enabled = false
end)
local function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
local function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
if Raycast and ammo > 0 then
local Hit:Part = Raycast.Instance
local Zombie = (Hit.Parent:FindFirstChild("Zombie") or Hit.Parent.Parent:FindFirstChild("Zombie"))
if Zombie then
UntagHumanoid(Zombie)
TagHumanoid(Zombie, Player)
Zombie.Health = Zombie.Health -Do.Damage
end
end
end
end)