A long time ago a coded a weapon script for my game.
And now looking back, it breaks quite often and is just really messy overall.
I want to see how I can make it break less often and how I can make it less messy without ruining the functionality.
I need to check what weapon it is so I can see the stats I need to use and need to create separate ammo for players just so one player can’t use every other player’s ammo.
But I haven’t done this in the most efficient way.
What way can I do to make it more efficient?
Weapon script
local rt = game:GetService("ReplicatedStorage"):WaitForChild("Shoot")
local reloadrt = game.ReplicatedStorage.Reload
local debris = game:GetService("Debris")
local debounce = false
--game.Players.PlayerAdded:Connect(function(plr)
-- task.wait(1)
-- print("aaa")
-- print("v nnbbb")
--end)
game.Players.PlayerAdded:Connect(function(plr)
valueak47 = Instance.new("IntValue")
valueak47.Value = 16
valueak47.Name = plr.Name .. "ak47ammo"
valueak47.Parent = script.Configuration["AK-47TOOL"]
valuepistol = Instance.new("IntValue")
valuepistol.Value = 16
valuepistol.Name = plr.Name .. "pistolammo"
valuepistol.Parent = script.Configuration["Pistol"]
valuesniper = Instance.new("IntValue")
valuesniper.Value = 2
valuesniper.Name = plr.Name .. "sniperammo"
valuesniper.Parent = script.Configuration["Sniper"]
valuesmg = Instance.new("IntValue")
valuesmg.Value = 20
valuesmg.Name = plr.Name .. "smgammo"
valuesmg.Parent = script.Configuration["GreaseGun"]
valueminigun = Instance.new("IntValue")
valueminigun.Value = 20
valueminigun.Name = plr.Name .. "minigunammo"
valueminigun.Parent = script.Configuration["Minigun"]
valueglock = Instance.new("IntValue")
valueglock.Value = 16
valueglock.Name = plr.Name .. "glockammo"
valueglock.Parent = script.Configuration["Glock"]
valuerevolver = Instance.new("IntValue")
valuerevolver .Value = 1
valuerevolver .Name = plr.Name .. "revolverammo"
valuerevolver .Parent = script.Configuration["Revolver"]
end)
rt.OnServerEvent:Connect(function(plr, mouse, tool,pos)
print("aaadfsss")
local isnil = false
local ammoout = false
local configfolders = script.Configuration
local reload = game.SoundService["Reload Sound"]
local gunshot = game:GetService("SoundService"):WaitForChild("Gun Shot")
local bullets = plr.Bulletpack.Bullet
local char = plr.Character
local hum = char.Humanoid
local animator = hum.Animator
local effect = tool.Effect.MuzzleEffect
local foundchild = configfolders:FindFirstChild(tool.Name)
local damagedebounce = false
if debounce == false and bullets.Value > 0 and ammoout == false then
print("aaadfssshfhfhfhfhfhfhfhfhfh")
if foundchild.Name == "AK-47TOOL" then
script.Configuration["AK-47TOOL"]:FindFirstChild(plr.Name.."ak47ammo").Value -=1
elseif foundchild.Name == "Pistol" then
script.Configuration["Pistol"]:FindFirstChild(plr.Name.."pistolammo").Value -=1
elseif foundchild.Name == "Sniper" then
script.Configuration["Sniper"]:FindFirstChild(plr.Name.."sniperammo").Value -=1
elseif foundchild.Name == "SMG" then
script.Configuration["GreaseGun"]:FindFirstChild(plr.Name.."smgammo").Value -=1
elseif foundchild.Name == "Minigun" then
script.Configuration["Minigun"]:FindFirstChild(plr.Name.."minigunammo").Value -=1
elseif foundchild.Name == "Glock" then
script.Configuration["Glock"]:FindFirstChild(plr.Name.."glockammo").Value -=1
elseif foundchild.Name == "Revolver" then
script.Configuration["Revolver"]:FindFirstChild(plr.Name.."revolverammo").Value -=1
end
bullets.Value -=1
print(bullets.Value)
print(foundchild.Ammo.Value)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://13112812781"
local animationid = foundchild:WaitForChild("AnimId").Value
local reloadanimationid = foundchild.ReloadAnim.Value
local animation = Instance.new("Animation")
animation.AnimationId = animationid
local animator = plr.Character.Humanoid.Animator
local loadanim = animator:LoadAnimation(animation)
loadanim:Play()
effect.Enabled = true
gunshot:Play()
if script.Configuration["AK-47TOOL"]:FindFirstChild(plr.Name.."ak47ammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
---loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["AK-47TOOL"]:FindFirstChild(plr.Name.."ak47ammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["AK-47TOOL"]:FindFirstChild(plr.Name.."ak47ammo").Value >= bullets.Value then
script.Configuration["AK-47TOOL"]:FindFirstChild(plr.Name.."ak47ammo").Value = bullets.Value
else
script.Configuration["AK-47TOOL"]:FindFirstChild(plr.Name.."ak47ammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
if script.Configuration["Pistol"]:FindFirstChild(plr.Name.."pistolammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["Pistol"]:FindFirstChild(plr.Name.."pistolammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["Pistol"]:FindFirstChild(plr.Name.."pistolammo").Value >= bullets.Value then
script.Configuration["Pistol"]:FindFirstChild(plr.Name.."pistolammo").Value = bullets.Value
else
script.Configuration["Pistol"]:FindFirstChild(plr.Name.."pistolammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
if script.Configuration["Sniper"]:FindFirstChild(plr.Name.."sniperammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["Sniper"]:FindFirstChild(plr.Name.."sniperammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["Sniper"]:FindFirstChild(plr.Name.."sniperammo").Value >= bullets.Value then
script.Configuration["Sniper"]:FindFirstChild(plr.Name.."sniperammo").Value = bullets.Value
else
script.Configuration["Sniper"]:FindFirstChild(plr.Name.."sniperammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
if script.Configuration["GreaseGun"]:FindFirstChild(plr.Name.."smgammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
---loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["GreaseGun"]:FindFirstChild(plr.Name.."smgammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["GreaseGun"]:FindFirstChild(plr.Name.."smgammo").Value >= bullets.Value then
script.Configuration["GreaseGun"]:FindFirstChild(plr.Name.."smgammo").Value = bullets.Value
else
script.Configuration["GreaseGun"]:FindFirstChild(plr.Name.."smgammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
if script.Configuration["Minigun"]:FindFirstChild(plr.Name.."minigunammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
---loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["Minigun"]:FindFirstChild(plr.Name.."minigunammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["Minigun"]:FindFirstChild(plr.Name.."minigunammo").Value >= bullets.Value then
script.Configuration["Minigun"]:FindFirstChild(plr.Name.."minigunammo").Value = bullets.Value
else
script.Configuration["Minigun"]:FindFirstChild(plr.Name.."minigunammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
if script.Configuration["Glock"]:FindFirstChild(plr.Name.."glockammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
---loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["Glock"]:FindFirstChild(plr.Name.."glockammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["Glock"]:FindFirstChild(plr.Name.."glockammo").Value >= bullets.Value then
script.Configuration["Glock"]:FindFirstChild(plr.Name.."glockammo").Value = bullets.Value
else
script.Configuration["Glock"]:FindFirstChild(plr.Name.."glockammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
if script.Configuration["Revolver"]:FindFirstChild(plr.Name.."revolverammo").Value == 0 then
reload:Play()
print(foundchild.Ammo.Value)
local animationreload = Instance.new("Animation")
animationreload.AnimationId = reloadanimationid
local loadanim = animator:LoadAnimation(animationreload)
---loadanim:Play()
ammoout = true
effect.Enabled = false
while true do
script.Configuration["Revolver"]:FindFirstChild(plr.Name.."revolverammo").Value = 0
debounce = true
task.wait(foundchild.ReloadTime.Value)
debounce = false
break
end
if script.Configuration["Revolver"]:FindFirstChild(plr.Name.."revolverammo").Value >= bullets.Value then
script.Configuration["Revolver"]:FindFirstChild(plr.Name.."revolverammo").Value = bullets.Value
else
script.Configuration["Revolver"]:FindFirstChild(plr.Name.."revolverammo").Value = foundchild.MaxAmmo.Value
end
reload:Stop()
ammoout = false
end
debounce = true
if mouse == nil then
task.wait(foundchild.FireRate.Value)
gunshot:Stop()
debounce = false
effect.Enabled = false
return
end
if mouse.Parent:FindFirstChild("Humanoid") then
mouse.Parent.Humanoid:TakeDamage(foundchild.Damage.Value)
debounce = true
elseif mouse.Parent.ClassName == "Accessory" then
mouse.Parent.Parent.Humanoid:TakeDamage(foundchild.Damage.Value)
debounce = true
end
debounce = true
task.wait(foundchild.FireRate.Value)
print("ay" .. foundchild.Name)
print(foundchild.FireRate.Value)
gunshot:Stop()
debounce = false
effect.Enabled = false
end
end)