Hey, im new to developing, and i created my first gun, any feedback for the gun and script cleaning/preformance?
Here is my code:
local gun = script.Parent
local shootRe = gun:WaitForChild("ShootRE")
local plr = nil
local m = nil
local connection = nil
local config = gun:WaitForChild("Configuration")
local useCooldown = false
local infAmmo = config.InfAmmo.Value
local cooldown = config:WaitForChild("Cooldown")
local canShoot = true
local ammo = config.Ammo
local tAmmo = config.TotalAmmo
local ammoPerReload = config.AmmoPerReload.Value
local clickSnd = gun.GunPart.SafetyClick
local magin = gun.GunPart.MagIn
local magout = gun.GunPart.MagOut
local canReload = true
local reloading = false
local realisticReload = config.RealisticReload.Value
local bulletsShooted = 0
local plrr = game.Players.LocalPlayer
local ui = plrr.PlayerGui.GunUi.Frame
local ammoUi = ui.ammo
local tAmmoUi = ui.tammo
local plrr = game.Players.LocalPlayer
local char = plrr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local anima = hum:WaitForChild("Animator")
local anim = Instance.new("Animation")
anim.AnimationId = script.Reload.AnimationId
local track = anima:LoadAnimation(anim)
local uis = game:GetService("UserInputService")
if infAmmo then
tAmmo.Value = math.huge
end
ammo.Value = ammoPerReload
tAmmoUi.Text = tAmmo.Value
ammoUi.Text = ammo.Value
local function onActivated()
if canShoot and not reloading then
local plrr = game.Players.LocalPlayer
local char = plrr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local anima = hum:WaitForChild("Animator")
local anim = Instance.new("Animation")
anim.AnimationId = script.Shoot.AnimationId
local track = anima:LoadAnimation(anim)
track:Play()
ammo.Value -= 1
bulletsShooted += 1
print("Ammo: "..ammo.Value)
ammoUi.Text = ammo.Value
shootRe:FireServer(m.Target)
if useCooldown then
canShoot = false
wait(cooldown.Value)
canShoot = true
end
elseif not canShoot and not reloading then
clickSnd:Play()
end
end
local function onEquip()
plr = game.Players.LocalPlayer
m = plr:GetMouse()
connection = gun.Activated:Connect(onActivated)
end
local function onUnquipped()
plr = nil
m = nil
connection:Disconnect()
end
gun.Equipped:Connect(onEquip)
gun.Unequipped:Connect(onUnquipped)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R and ammo.Value ~= ammoPerReload and canReload then
if realisticReload then
print("reloading (realistic)")
canReload = false
canShoot = false
reloading = true
track:Play()
wait(.14)
magout:Play()
wait(1.04)
wait(.14)
magin:Play()
canShoot = true
reloading = false
ammo.Value = ammoPerReload
print("ammo: "..ammo.Value)
tAmmo.Value -= ammoPerReload
print("tAmmo: "..tAmmo.Value)
ammoUi.Text = ammo.Value
tAmmoUi.Text = tAmmo.Value
canReload = true
else
print("reloading (normal)")
canReload = false
canShoot = false
reloading = true
track:Play()
wait(.14)
magout:Play()
wait(1.04)
wait(.14)
magin:Play()
canShoot = true
reloading = false
ammo.Value = ammoPerReload
print("ammo: "..ammo.Value)
tAmmo.Value -= bulletsShooted
bulletsShooted = 0
print("tAmmo: "..tAmmo.Value)
ammoUi.Text = ammo.Value
tAmmoUi.Text = tAmmo.Value
canReload = true
end
if tAmmo.Value < 1 then
ammoPerReload = 0
tAmmo.Value = 0
tAmmoUi.Text = tAmmo.Value
end
if tAmmo.Value < 1 and ammo.Value < 1 then
canShoot = false
ammo.Value = 0
ammoUi.Text = ammo.Value
track:Stop()
magin:Stop()
magout:Stop()
end
end
end)
while wait() do
if ammo.Value < 1 then
canShoot = false
end
end
Also i have a serverscript for handling the damage:
local shootRe = script.Parent:WaitForChild("ShootRE")
local dmg = script.Parent.Configuration.Damage.Value
local snd = script.Parent.GunPart:WaitForChild("Fire")
local config = script.Parent:WaitForChild("Configuration")
local cooldown = config:WaitForChild("Cooldown")
local canShoot = true
local smokeFlash = script.Parent.GunPart.FlashAtt.Smoke
local FlashFx = smokeFlash.Parent["FlashFX[Flash]"]
local ts = game:GetService("TweenService")
local blur = game.Lighting.sBlur
local info = TweenInfo.new(.17, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
local infoO = TweenInfo.new(.5, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
local goal = {Size = 10}
local goalOut = {Size = 0}
local trackIn = ts:Create(blur, info, goal)
local trackOut = ts:Create(blur, infoO, goalOut)
local function tagHum(hum, plr)
local cTag = Instance.new("ObjectValue", hum)
cTag.Name = "creator"
cTag.Value = plr
game.Debris:AddItem(cTag, 2)
end
local function unTagHum(hum)
for i, v in pairs(hum:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
local function onShoot(plr, target)
if canShoot then
snd:Play()
FlashFx:Emit(1000)
FlashFx:Emit(1000)
if target and target.Parent then
local hum = target.Parent:FindFirstChild("Humanoid")
if hum then
unTagHum(hum)
tagHum(hum, plr)
hum:TakeDamage(dmg)
canShoot = false
if hum.Health < 20 then
trackIn:Play()
wait(.1)
trackOut:Play()
end
end
end
wait(cooldown.Value)
canShoot = true
end
end
shootRe.OnServerEvent:Connect(onShoot)
Thanks!
Video: https://youtu.be/OAgeRkccN2M