Hello!
After following a bit of tutorials i have made a semi-automatic assault rifle. The thing is. after everything was coded it stopped working on me. There were no errors popping up, and i couldn’t find the issue. I would love to recieve some help. Thanks!
– Shoot –
local re = script.Parent:WaitForChild("RemoteEvent")
local player= nil
local mouse = nil
local connection = nil
local kickBack = script.Parent:WaitForChild("Kickback")
local kickTrack = nil
local canShoot = true
local coolDown = .3
local rs = game:GetService("ReplicatedStorage")
local gunScreenGuiTemplate = script.Parent:WaitForChild("GunScreenGui")
local gunScreenGui = nil
local ammoLbl = nil
local ammo = 30
local maxAmmo = 30
local click = script.Parent.Handle:WaitForChild("Click")
local reloadSnd = script.Parent.Handle:WaitForChild("Reload")
local isReloading = false
local uis = game:GetService("UserInputService")
local reloadAnim = script.Parent.ReloadAnim:WaitForChild("ReloadAnim")
local reloadTrack = nil
local reloadKey = Enum.KeyCode.R
local function reload()
isReloading = true
reloadSnd:Play()
reloadTrack:Play()
wait(reloadTrack.Length)
ammo = maxAmmo
ammoLbl.Text = "Ammo: " .. ammo .. "/" .. maxAmmo
isReloading = false
end
local function makeAmmoGui()
local playerGui = player:WaitForChild("PlayerGui")
gunScreenGui = playerGui:FindFirstChild("GunScreenGui")
if not gunScreenGui then
gunScreenGui = gunScreenGuiTemplate:Clone()
gunScreenGui.Parent = playerGui
end
gunScreenGui.Enabled = true
ammoLbl = gunScreenGui:FindFirstChild("AmmoLbl")
ammoLbl.Text = "Ammo: " .. ammo .. "/" .. maxAmmo
end
local function onActivated()
if canShoot and ammo > 0 and not isReloading then
canShoot = false
kickTrack:Play()
re:FireServer(mouse.Target)
ammo -= 1
ammoLbl.Text = "Ammo: " .. ammo .. "/" .. maxAmmo
wait(coolDown)
canShoot = true
elseif ammo <= 0 and not isReloading then
--click:Play()
reload()
end
end
local function onEquipped()
player = game.Players.LocalPlayer
mouse = player:GetMouse()
connection = gun.Activated:Connect(onActivated)
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
kickTrack = hum.Animator:LoadAnimation(kickBack)
reloadTrack = hum.Animator:LoadAnimation(reloadAnim)
makeAmmoGui()
end
local function onUnequipped()
player = nil
mouse = nil
connection:Disconnect()
if gunScreenGui then
gunScreenGui.Enabled = false
end
end
gun.Equipped:Connect(onEquipped)
gun.Equipped:Connect(onUnequipped)
uis.InputBegan:Connect(function(input, gameProcess)
if input.KeyCode == reloadKey and
ammo ~= maxAmmo and not isReloading then
reload()
end
end)
-- Damage --
local re = script.Parent:WaitForChild("RemoteEvent")
local damage = 20
local bang = script.Parent.Handle.GunShot
local function onShoot(player, target)
bang:Play()
if target and target.Parent then
local hum = target.Parent:FindFirstChild("Humanoid")
if hum then
hum:TakeDamage(damage)
end
end
end
re.OnServerEvent:Connect(onShoot)