Scripting Support on a Weapon

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)
1 Like

Maybe try adding some warns and prints to help you track through the code. That way you can see any problems in the Output window.

local function onShoot(player, target)
	
	print("Player =", player)
	
	bang:Play()
	if target and target.Parent then
		local hum = target.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(damage)
		else
			
			warn("No hum.")
			
		end
	else
		
		warn("No target or target.Parent", target, target.Parent)
		
	end
end

tried doing so and nothing helped. i even had a GUI for ammo counts and reloading stuff. I think i have to rescript everything because it broke on me

1 Like

Silent errors, the worst of all, use print before every scope to see what happens

If nothing helped then rewriting should be good option, usually this mean there is buggy silent error that is very hard to fix, try to keep your code redable and you will be fine

1 Like

Thanks alot, i will try em out!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.