Gun reload problem

Problem 1 : When i reload while some ammo still here, it work fine but if there no ammo then it won’t reload the gun

Problem 2 : After out of ammo, the animation still play yet it unequipped

here the local script :

local userInput = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
function GetMousePosition(X, Y)
	local cameraRay= camera:ViewportPointToRay(X, Y)
	local newRay = Ray.new(cameraRay.Origin, cameraRay.Direction*1000000)
	local target, position = workspace:FindPartOnRay(newRay, player.Character)
	return position
end

local tool = script.Parent
local remote = tool:WaitForChild("RemoteEvent")

local equipped = false 

local Ammo = 30
local MaxAmmo = 30

local Reloading = false

tool.Equipped:Connect(function()
equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

local fired = false
local fireRate = 0.125
local fireTick = tick()

userInput.InputBegan:connect(function(input,gui)
	if not equipped then return end
	if gui then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if tick()-fireTick < fireRate then return end
		fireTick = tick()
		fired = true
		while fired do
			local location = userInput:GetMouseLocation()
			remote:FireServer(GetMousePosition(location.x,location.y))
			task.wait(fireRate)
			if Reloading == false then
				if Ammo > 0 then
					Ammo -= 1
					player.PlayerGui.AmmoGui.Frame.Ammo.Text = Ammo.."/30"
					game.ReplicatedStorage.ReplicateBullets:FireServer()
					if Ammo == 0 then
						fired = false
						script.Disabled = true
					end
				end
			end
		end
	end
end)

script.Enabled = true
userInput.InputBegan:Connect(function(input)
	script.Enabled = true
	if input.KeyCode == Enum.KeyCode.R then
		if Reloading == false or Ammo >- 0 then
			Reloading = true
			script.Parent.Reload:Play()
			wait(5)
			Ammo = 30
			player.PlayerGui.AmmoGui.Frame.Ammo.Text = Ammo.."/30"
			Reloading = false
			script.Disabled = false
		end
	end
end)

userInput.InputEnded:Connect(function(input,gui)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		fired = false
	end
end)

script.Parent.Equipped:Connect(function()
	player.PlayerGui.AmmoGui.Enabled = true

end)
script.Parent.Unequipped:Connect(function()
	player.PlayerGui.AmmoGui.Enabled = false

end)

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local h = character:WaitForChild("Humanoid")
local anim = h:LoadAnimation(script.Parent:WaitForChild("Hold"))

local tool = script.Parent

tool.Equipped:Connect(function()
	anim:Play()
end)

tool.Unequipped:Connect(function()
	anim:Stop()
end)

and this is the script

local tool = script.Parent
local remoteEvent = tool.RemoteEvent

local equipped = false        

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

local fireRate = .1
local timeLimit = 2/3

local eventLimit = 10
local events = 0

local fireTick = tick()
local eventTick = tick()

remoteEvent.OnServerEvent:Connect(function(player,mousePos)
	if not equipped then return end
	if game.Players:GetPlayerFromCharacter(tool.Parent) ~= player then
		return
	end
	local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health <= 0 then
		return
	end
	if not ((tick()-fireTick) >= fireRate*timeLimit) then return end
	fireTick = tick()
	if events >= eventLimit and tick()-eventLimit < 1 then
		return
	end
	if tick()-eventLimit >= 1 then
		eventTick = tick()
		events = 0
	end
	events += 1
	fire(player,mousePos)
end)

local bulletReplicator = game.ReplicatedStorage.ReplicateBullets
local handle = tool.Handle
function fire(player,mousePos)
	local startPos = handle.Muzzle.WorldPosition
	local velocity = 1650
	local direction = CFrame.new(startPos,mousePos)
	direction = direction.lookVector*velocity

	local range = 3000

	local sound = Instance.new("Sound",script.Parent.Handle)
	sound.SoundId = "rbxassetid://2047319236"
	sound:Play()
	sound.Ended:Connect(function()
		sound:Destroy()
	end)

	bulletReplicator:FireAllClients(player.Character,startPos,direction,range)

	local startTime = tick()
	local lastPos = startPos

	local blacklist = {player.Character}
	local rayFilter = RaycastParams.new()
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist

	for _,char in pairs(workspace:GetChildren()) do
		if char:FindFirstChildOfClass("Humanoid") then
			for _,accessory in pairs(char:GetChildren()) do
				if accessory:IsA("Accessory") then
					table.insert(blacklist,accessory.Handle)
				end
			end
		end
	end

	rayFilter.FilterDescendantsInstances = blacklist

	while range > 0 do
		local timeLength = (tick()-startTime)

		local currentPos = startPos + (direction*timeLength)

		local distance = (lastPos-currentPos).Magnitude
		range -= distance

		local ray = workspace:Raycast(lastPos,currentPos-lastPos,rayFilter)
		if ray == nil then
			ray = workspace:Raycast(currentPos,lastPos-currentPos,rayFilter)
		end
		if ray then
			local hit = ray.Instance
			currentPos = ray.Position
			local model = hit:FindFirstAncestorOfClass("Model")
			if model then
				local Humanoid = model:FindFirstChildWhichIsA("Humanoid")
				if Humanoid then
					if hit.Name == "Head" then
						Humanoid:TakeDamage(120)
					else
						Humanoid:TakeDamage(54)
					end
				end
			end
			break
		end
		lastPos = currentPos
		task.wait()
	end
end
3 Likes

As you can see that clip, if ammo still inside magazine then it reloadable. But when theres none, it won’t reload

Well i try help in Gui Problem If you Reload {Press Like r} you call in from script gun Event Then You add add Event then you add in script “ReplicatedStorage” Then you readying something in this script and you Read the textScale Like “Reloiding” and maybe its work i dont no you can try

1 Like

@TomaQueen Gui this Fix or no?

not yet but thanks for trying helping

I think the animation problem is because you have 2 unequipped functions try only using 1
also in this part

if Reloading == false or Ammo >- 0 then

try using Ammo <= 0 insted of Ammo >- 0

Cheers.

i replaced it but it still not work after empty ammo

if Reloading == false then
				if Ammo > 0 or Ammo <= 0 then

it look likes it doesn’t changes but i think the

script.Disabled = true

might be problem

oh wait i made a mistake there, my bad

yeah thats probably the problem does it work now?

im finding way to replace the script.Disabled = true because fired = false won’t work

Nevermind i fixed problem on my own, thank you guys for trying to help me btw

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