I made a gun that has 3 ammo, which one-shots, however It appears to not damage a player nor NPC whenever the user clicks on a player or NPC with the gun equipped
Script (Main script, local script):
local maxammo = 3
local ammo = maxammo
local reloading = false
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local text = plrgui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local ShootingAnimation = script.Parent.Animation
local ReloadAnimation = script.Parent.Reload
local function reload()
reloading = true
script.Parent.reloadingsound:Play() -- change reloadingsound to what ever your reload sound is named
local Char = script.Parent.Parent
local Hum = Char.Humanoid
local AnimationTrack = Hum:LoadAnimation(ReloadAnimation)
AnimationTrack:Play()
wait(1)
ammo = maxammo
reloading = false
end
script.Parent.Activated:Connect(function(Mouse)
if ammo > 0 and not reloading then
local Char = script.Parent.Parent
local Hum = Char.Humanoid
local AnimationTrack = Hum:LoadAnimation(ShootingAnimation)
AnimationTrack:Play()
ammo = ammo - 1
script.Parent.gunshot:Play() -- change gunshot to what ever your gun shot sound is named
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 30) -- change the 25 to what ever amount of damage you want to deal
end
elseif reloading == false then
reload()
script.Parent.gunshot:Stop()-- change gunshot to what ever your gun shot sound is named
end
while wait() do
text.Text = (ammo).." / "..maxammo
end
end)
script.Parent.Equipped:Connect(function()
local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxammo then
reload()
end
end)
end)
script.Parent.Equipped:Connect(function()
game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = true
script.Parent.Unequipped:Connect(function()
game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = false
end)
end)
Script (Damage script, server script):
script.Parent.DealDamage.OnServerEvent:Connect(function(player, Target, Damage)
Target.Humanoid:TakeDamage(Damage)
end)
