Tool Glitch | Support

Hey!

I noticed a bug in my game and am not sure how to fix it.
Below is a video of the issue:
(I am equipping the gun, and then unequipping it)

Code:

repeat wait() until game:IsLoaded()

local gun = script.Parent
local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()
task.wait(0.3)
local gui = script.Parent.M9Gui
local lp = game.Players.LocalPlayer
local txt = gui.FullFrame.TextLabel
local handle = script.Parent.Handle
local alwaysfp = false -- set this to true if you want to be in first person always
local equipfp = false -- set this true if you want to go into first person while holding the gun
local ret = false -- set this to false if you don't want a custom reticle and want to keep the default cursor
local reticle = "rbxassetid://2130621557" -- change this to the reticle you want
local reticleHM = "rbxassetid://2167171797" -- change this to the hit marker you want
local magsize = 30 -- how many bullets it can hold
local bullets = 30 -- how many bullets are in the mag
local reloading = false -- is the gun reloading or not
local maxrange = 100 -- max range the bullets can go
local fireRate = 0.1 -- fire rate in seconds (lower value means faster fire rate)
local reloadtime = 3 -- how long it takes to reload
local reloadkey = "r" -- what button to press to reload
local isFiring = false -- indicates whether the gun is firing or not
local holdanim1 = Instance.new("Animation")
local shootanim1 = Instance.new("Animation")
local aiminanim1 = Instance.new("Animation")
local reloadanim1 = Instance.new("Animation")
holdanim1.AnimationId = "rbxassetid://14089674542"
shootanim1.AnimationId = "rbxassetid://14089811993"
aiminanim1.AnimationId = "rbxassetid://14089811993"
reloadanim1.AnimationId = "rbxassetid://14213097033"
local holdanim = lp.Character.Humanoid:LoadAnimation(holdanim1)
local shootanim = lp.Character.Humanoid:LoadAnimation(shootanim1)
local aiminanim = lp.Character.Humanoid:LoadAnimation(aiminanim1)
local reloadanim = lp.Character.Humanoid:LoadAnimation(reloadanim1)
print("Logging: \\ 1") -- PrintLogging

gui.Parent = player.PlayerGui
txt.Text = bullets .. "/" .. magsize

if alwaysfp then
	player.CameraMode = Enum.CameraMode.LockFirstPerson
else
	player.CameraMode = Enum.CameraMode.Classic
end

local function bulletCos(hit, pos, flush, hole)
	if hole then
		--local bullethole = Instance.new("Part")
		--bullethole.FormFactor = "Custom"
		--bullethole.Size = Vector3.new(1, 1, 0.2)
		--bullethole.Anchored = true
		--bullethole.CanCollide = false
		--bullethole.Transparency = 1
		--local bulletholedecal = Instance.new("Decal", bullethole)
		--bulletholedecal.Face = "Front"
		--bulletholedecal.Texture = "http://www.roblox.com/asset/?id=2078626"
		--bullethole.Parent = game.Workspace
		--bullethole.CFrame = CFrame.new(pos, pos + flush)
		--game:GetService("Debris"):AddItem(bullethole, 3)
	end
	--[[ -- New Shoot Raycasting ]]
	local trail = Instance.new("Part")
	trail.BrickColor = BrickColor.new("Smoky grey")
	trail.FormFactor = "Custom"
	trail.Size = Vector3.new(0.01, 0.01, (gun.Handle.Front.Position - pos).Magnitude)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Transparency = 0.9
	trail.Material = Enum.Material.Neon
	trail.CFrame = CFrame.new(gun.Handle.Front.Position, pos) * CFrame.new(0, 0, -(gun.Handle.Front.Position - pos).Magnitude / 2)
	trail.Parent = game.Workspace
	game:GetService("Debris"):AddItem(trail, fireRate)
	
	--[[ -- Old Shoot Raycasting
	local trail = Instance.new("Part")
	trail.BrickColor = BrickColor.new("Fog")
	trail.FormFactor = "Custom"
	trail.Size = Vector3.new(0.01, 0.01, (gun.Handle.Front.Position - pos).Magnitude)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Transparency = 0.75
	trail.Material = Enum.Material.Plastic
	trail.CFrame = CFrame.new(gun.Handle.Front.Position, pos) * CFrame.new(0, 0, -(gun.Handle.Front.Position - pos).Magnitude / 2)
	trail.Parent = game.Workspace
	game:GetService("Debris"):AddItem(trail, fireRate)
	-]]
end

local equipd = false
local UserInputService = game:GetService("UserInputService")

gun.Equipped:Connect(function()
	print("Logging: \\ 2") -- PrintLogging
	
	
	
	-- Ev's Stuff

	local FinalPlayer = player.Name
	game.Players:FindFirstChild(FinalPlayer).OTSView.Value = true
	
	
	

	holdanim:Play()
	equipd = true
	if ret then
		mouse.Icon = reticle
	else
		mouse.Icon = ""
	end
	if equipfp then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		player.CameraMode = Enum.CameraMode.Classic
	end
	gui.Enabled = true

	local canFire = true -- variable to track if the gun can fire

	local function Fire()
		print("Logging: \\ 3") -- PrintLogging
		holdanim:Stop()
		shootanim:Play()

		if canFire then
			canFire = false -- disable firing temporarily
			while isFiring and bullets > 0 and not reloading and equipd do
				bullets = bullets - 1
				handle.GunShot:Play()
				txt.Text = bullets .. "/" .. magsize
				local ray = Ray.new(gun.Handle.Front.Position, (mouse.Hit.p - gun.Handle.Front.Position).Unit * maxrange)
				local hit, position, flush = workspace:FindPartOnRay(ray, player.Character)
				if hit then
					local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
					if humanoid then
						bulletCos(hit, position, flush, false)
						script.Parent.BulletHit2:FireServer(humanoid)
						spawn(function()
							mouse.Icon = reticleHM
							wait(0.2)
							mouse.Icon = reticle
						end)
					else
						bulletCos(hit, position, flush, true)
					end
				end
				bulletCos(hit, position, flush, false)
				wait(fireRate)
			end
			wait(fireRate) -- add additional delay before enabling firing again
			canFire = true -- enable firing
		end
	end
	gun.Equipped:Connect(function()
		mouse.Button1Down:Connect(function()
			if equipd then -- Check if the gun is equipped before firing
				isFiring = true
				Fire()
			end
		end)

		mouse.Button1Up:Connect(function()
			if equipd then
				isFiring = false
				shootanim:Stop()
				holdanim:Play()
			end
		end)

		mouse.Button2Down:Connect(function()
			if equipd then
				shootanim:Stop()
				holdanim:Stop()
				aiminanim:Play()
			end
		end)

		mouse.Button2Up:Connect(function()
			if equipd then
				aiminanim:Stop()
				holdanim:Play()
			end
		end)

		mouse.KeyDown:Connect(function(key)
			if key == reloadkey and not reloading and equipd then
				reloading = true
				txt.Text = "Reloading"
				holdanim:Stop()
				shootanim:Stop()
				aiminanim:Stop()
				handle.Reload1:Play()
				reloadanim:Play()
				
				wait(reloadtime)
				bullets = magsize
				reloading = false
				txt.Text = bullets .. "/" .. magsize
				holdanim:Play()
			end
		end)
	end)
end)
gun.Unequipped:Connect(function()

	-- Ev's Stuff
	print("Logging: \\ 4") -- PrintLogging
	
	local FinalPlayer = player.Name
	game.Players:FindFirstChild(FinalPlayer).OTSView.Value = false
	
	--


	equipd = false
	if ret then
		mouse.Icon = ""
	end
	if equipfp and not alwaysfp then
		player.CameraMode = Enum.CameraMode.Classic
		player.CameraMinZoomDistance = player.CameraMaxZoomDistance / 10
		wait()
		player.CameraMinZoomDistance = 0.5
	elseif alwaysfp then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	end
	gui.Enabled = false
	holdanim:Stop()
	shootanim:Stop()
	aiminanim:Stop()
	holdanim:Destroy()
	shootanim:Destroy()
	aiminanim:Destroy()
	
	
	
	--[[  Attenpt To Fix  ]]
	local magsize = 30
	local bullets = 30
	local reloading = false
	local maxrange = 100
	local fireRate = 0.1
	local reloadtime = 3
	--[[  End   ]]
	print("Logging: \\ 5") -- PrintLogging
end)

Let me know if you have an idea!

1 Like

I used to have this problem all the time. It may be script still running code, by Unequipped function or a loop.
If you don’t break or return a loop, it will stack and keep stacking. So basically when the loop starts, it won’t stop unless you break or return it. So when you Unequip the Tool, the loop is still running. When you equip the tool again and start the loop again it stacks with the already running loop. So now its 2x faster and so forth the more you unequip and equip again.

Try Replacing Code with this and see if it helps.

while wait(canFire) do
	if  isFiring and bullets > 0 and not reloading and equipd then
	bullets = bullets - 1
	handle.GunShot:Play()
	txt.Text = bullets .. "/" .. magsize
	local ray = Ray.new(gun.Handle.Front.Position, (mouse.Hit.p - gun.Handle.Front.Position).Unit * maxrange)
	local hit, position, flush = workspace:FindPartOnRay(ray, player.Character)
	if hit then
		local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
		if humanoid then
			bulletCos(hit, position, flush, false)
			script.Parent.BulletHit2:FireServer(humanoid)
			spawn(function()
				mouse.Icon = reticleHM
				wait(0.2)
				mouse.Icon = reticle
			end)
		else
			bulletCos(hit, position, flush, true)
		end
	end
	bulletCos(hit, position, flush, false)
		--wait(fireRate)
	else
		break
	end
end	
wait(fireRate) -- add additional delay before enabling firing again
canFire = true -- enable firing
end
1 Like

Maybe give this a try

repeat wait() until game:IsLoaded()

local gun = script.Parent
local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()
local gui = script.Parent.M9Gui
local lp = game.Players.LocalPlayer
local txt = gui.FullFrame.TextLabel
local handle = script.Parent.Handle
local alwaysfp = false
local equipfp = false
local ret = false
local reticle = “rbxassetid://2130621557”
local reticleHM = “rbxassetid://2167171797”
local magsize = 30
local bullets = 30
local reloading = false
local maxrange = 100
local fireRate = 0.1
local reloadtime = 3
local reloadkey = “r”
local isFiring = false
local holdanim1 = Instance.new(“Animation”)
local shootanim1 = Instance.new(“Animation”)
local aiminanim1 = Instance.new(“Animation”)
local reloadanim1 = Instance.new(“Animation”)
holdanim1.AnimationId = “rbxassetid://14089674542”
shootanim1.AnimationId = “rbxassetid://14089811993”
aiminanim1.AnimationId = “rbxassetid://14089811993”
reloadanim1.AnimationId = “rbxassetid://14213097033”
local holdanim = lp.Character.Humanoid:LoadAnimation(holdanim1)
local shootanim = lp.Character.Humanoid:LoadAnimation(shootanim1)
local aiminanim = lp.Character.Humanoid:LoadAnimation(aiminanim1)
local reloadanim = lp.Character.Humanoid:LoadAnimation(reloadanim1)

gui.Parent = player.PlayerGui
txt.Text = bullets … “/” … magsize

if alwaysfp then
player.CameraMode = Enum.CameraMode.LockFirstPerson
else
player.CameraMode = Enum.CameraMode.Classic
end

local function bulletCos(hit, pos, flush, hole)
– Code for bulletCos function goes here
end

local equipd = false
local UserInputService = game:GetService(“UserInputService”)

gun.Equipped:Connect(function()
– Code for Equipped event goes here
end)

gun.Unequipped:Connect(function()
– Code for Unequipped event goes here
end)

Also sme parts of the script are missing, such as the bulletCos function implementations,

Hope it helps!

2 Likes

Thanks for your quick reply! Which line(s) would I replace your code with?
Thanks.

1 Like

Mainly just the animation ID and bulletcos, I noted everything below so should be easier for you to read

repeat wait() until game:IsLoaded()

local gun = script.Parent
local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()
local gui = script.Parent.M9Gui
local lp = game.Players.LocalPlayer
local txt = gui.FullFrame.TextLabel
local handle = script.Parent.Handle
local alwaysfp = false
local equipfp = false
local ret = false
local reticle = “rbxassetid://2130621557”
local reticleHM = “rbxassetid://2167171797”
local magsize = 30
local bullets = 30
local reloading = false
local maxrange = 100
local fireRate = 0.1
local reloadtime = 3
local reloadkey = “r”
local isFiring = false
local holdanim1 = Instance.new(“Animation”)
local shootanim1 = Instance.new(“Animation”)
local aiminanim1 = Instance.new(“Animation”)
local reloadanim1 = Instance.new(“Animation”)
holdanim1.AnimationId = “rbxassetid://14089674542”
shootanim1.AnimationId = “rbxassetid://14089811993”
aiminanim1.AnimationId = “rbxassetid://14089811993”
reloadanim1.AnimationId = “rbxassetid://14213097033”
local holdanim = lp.Character.Humanoid:LoadAnimation(holdanim1)
local shootanim = lp.Character.Humanoid:LoadAnimation(shootanim1)
local aiminanim = lp.Character.Humanoid:LoadAnimation(aiminanim1)
local reloadanim = lp.Character.Humanoid:LoadAnimation(reloadanim1)

gui.Parent = player.PlayerGui
txt.Text = bullets … “/” … magsize

if alwaysfp then
player.CameraMode = Enum.CameraMode.LockFirstPerson
else
player.CameraMode = Enum.CameraMode.Classic
end

local function bulletCos(hit, pos, flush, hole)
– Code for bulletCos function goes here
end

local equipd = false
local UserInputService = game:GetService(“UserInputService”)

gun.Equipped:Connect(function()
print(“Equipped”) – Placeholder for your animation code to equip the tool
– Insert your animation code here
– For example:
– play your “equip” animation here
– play your sound effect here
equipd = true
end)

gun.Unequipped:Connect(function()
print(“Unequipped”) – Placeholder for your animation code to unequip the tool
– Insert your animation code here
– For example:
– play your “unequip” animation here
– play your sound effect here
equipd = false
end)

1 Like

Thanks, I am going to try @Trainmaster2341 's advice first, as he was quicker to respond.
I’ll let you know if it works!

1 Like

It don’t bother me if you do mine last or at all, as long as your problem is resolved. :smiley:

I just recommend breaking your loop. Like if equipd == false then break end in the while loop.
Anyways here is code with small adjustments(just added the break in the loop).

repeat wait() until game:IsLoaded()

local gun = script.Parent
local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()
task.wait(0.3)
local gui = script.Parent.M9Gui
local lp = game.Players.LocalPlayer
local txt = gui.FullFrame.TextLabel
local handle = script.Parent.Handle
local alwaysfp = false -- set this to true if you want to be in first person always
local equipfp = false -- set this true if you want to go into first person while holding the gun
local ret = false -- set this to false if you don't want a custom reticle and want to keep the default cursor
local reticle = "rbxassetid://2130621557" -- change this to the reticle you want
local reticleHM = "rbxassetid://2167171797" -- change this to the hit marker you want
local magsize = 30 -- how many bullets it can hold
local bullets = 30 -- how many bullets are in the mag
local reloading = false -- is the gun reloading or not
local maxrange = 100 -- max range the bullets can go
local fireRate = 0.1 -- fire rate in seconds (lower value means faster fire rate)
local reloadtime = 3 -- how long it takes to reload
local reloadkey = "r" -- what button to press to reload
local isFiring = false -- indicates whether the gun is firing or not
local holdanim1 = Instance.new("Animation")
local shootanim1 = Instance.new("Animation")
local aiminanim1 = Instance.new("Animation")
local reloadanim1 = Instance.new("Animation")
holdanim1.AnimationId = "rbxassetid://14089674542"
shootanim1.AnimationId = "rbxassetid://14089811993"
aiminanim1.AnimationId = "rbxassetid://14089811993"
reloadanim1.AnimationId = "rbxassetid://14213097033"
local holdanim = lp.Character.Humanoid:LoadAnimation(holdanim1)
local shootanim = lp.Character.Humanoid:LoadAnimation(shootanim1)
local aiminanim = lp.Character.Humanoid:LoadAnimation(aiminanim1)
local reloadanim = lp.Character.Humanoid:LoadAnimation(reloadanim1)
print("Logging: \\ 1") -- PrintLogging

gui.Parent = player.PlayerGui
txt.Text = bullets .. "/" .. magsize

if alwaysfp then
	player.CameraMode = Enum.CameraMode.LockFirstPerson
else
	player.CameraMode = Enum.CameraMode.Classic
end

local function bulletCos(hit, pos, flush, hole)
	if hole then
		--local bullethole = Instance.new("Part")
		--bullethole.FormFactor = "Custom"
		--bullethole.Size = Vector3.new(1, 1, 0.2)
		--bullethole.Anchored = true
		--bullethole.CanCollide = false
		--bullethole.Transparency = 1
		--local bulletholedecal = Instance.new("Decal", bullethole)
		--bulletholedecal.Face = "Front"
		--bulletholedecal.Texture = "http://www.roblox.com/asset/?id=2078626"
		--bullethole.Parent = game.Workspace
		--bullethole.CFrame = CFrame.new(pos, pos + flush)
		--game:GetService("Debris"):AddItem(bullethole, 3)
	end
	--[[ -- New Shoot Raycasting ]]
	local trail = Instance.new("Part")
	trail.BrickColor = BrickColor.new("Smoky grey")
	trail.FormFactor = "Custom"
	trail.Size = Vector3.new(0.01, 0.01, (gun.Handle.Front.Position - pos).Magnitude)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Transparency = 0.9
	trail.Material = Enum.Material.Neon
	trail.CFrame = CFrame.new(gun.Handle.Front.Position, pos) * CFrame.new(0, 0, -(gun.Handle.Front.Position - pos).Magnitude / 2)
	trail.Parent = game.Workspace
	game:GetService("Debris"):AddItem(trail, fireRate)

	--[[ -- Old Shoot Raycasting
	local trail = Instance.new("Part")
	trail.BrickColor = BrickColor.new("Fog")
	trail.FormFactor = "Custom"
	trail.Size = Vector3.new(0.01, 0.01, (gun.Handle.Front.Position - pos).Magnitude)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Transparency = 0.75
	trail.Material = Enum.Material.Plastic
	trail.CFrame = CFrame.new(gun.Handle.Front.Position, pos) * CFrame.new(0, 0, -(gun.Handle.Front.Position - pos).Magnitude / 2)
	trail.Parent = game.Workspace
	game:GetService("Debris"):AddItem(trail, fireRate)
	-]]
end

local equipd = false
local UserInputService = game:GetService("UserInputService")

gun.Equipped:Connect(function()
	print("Logging: \\ 2") -- PrintLogging



	-- Ev's Stuff

	local FinalPlayer = player.Name
	game.Players:FindFirstChild(FinalPlayer).OTSView.Value = true




	holdanim:Play()
	equipd = true
	if ret then
		mouse.Icon = reticle
	else
		mouse.Icon = ""
	end
	if equipfp then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		player.CameraMode = Enum.CameraMode.Classic
	end
	gui.Enabled = true

	local canFire = true -- variable to track if the gun can fire

	local function Fire()
		print("Logging: \\ 3") -- PrintLogging
		holdanim:Stop()
		shootanim:Play()

		if canFire then
			canFire = false -- disable firing temporarily
			while wait(fireRate) do
				if  isFiring and bullets > 0 and not reloading and equipd then
					bullets = bullets - 1
					handle.GunShot:Play()
					txt.Text = bullets .. "/" .. magsize
					local ray = Ray.new(gun.Handle.Front.Position, (mouse.Hit.p - gun.Handle.Front.Position).Unit * maxrange)
					local hit, position, flush = workspace:FindPartOnRay(ray, player.Character)
					if hit then
						local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
						if humanoid then
							
							bulletCos(hit, position, flush, false)
							script.Parent.BulletHit2:FireServer(humanoid)
							spawn(function()
								mouse.Icon = reticleHM
								wait(0.2)
								mouse.Icon = reticle
							end)
						else
							
							bulletCos(hit, position, flush, true)
						end
					end
					bulletCos(hit, position, flush, false)
					--wait(fireRate)
				else
					break
				end
			end	
			wait(fireRate) -- add additional delay before enabling firing again
			canFire = true -- enable firing
		end
	end
	gun.Equipped:Connect(function()
		mouse.Button1Down:Connect(function()
			if equipd then -- Check if the gun is equipped before firing
				isFiring = true
				Fire()
			end
		end)

		mouse.Button1Up:Connect(function()
			if equipd then
				isFiring = false
				shootanim:Stop()
				holdanim:Play()
			end
		end)

		mouse.Button2Down:Connect(function()
			if equipd then
				shootanim:Stop()
				holdanim:Stop()
				aiminanim:Play()
			end
		end)

		mouse.Button2Up:Connect(function()
			if equipd then
				aiminanim:Stop()
				holdanim:Play()
			end
		end)

		mouse.KeyDown:Connect(function(key)
			if key == reloadkey and not reloading and equipd then
				reloading = true
				txt.Text = "Reloading"
				holdanim:Stop()
				shootanim:Stop()
				aiminanim:Stop()
				handle.Reload1:Play()
				reloadanim:Play()

				wait(reloadtime)
				bullets = magsize
				reloading = false
				txt.Text = bullets .. "/" .. magsize
				holdanim:Play()
			end
		end)
	end)
end)
gun.Unequipped:Connect(function()

	-- Ev's Stuff
	print("Logging: \\ 4") -- PrintLogging

	local FinalPlayer = player.Name
	game.Players:FindFirstChild(FinalPlayer).OTSView.Value = false

	--


	equipd = false
	if ret then
		mouse.Icon = ""
	end
	if equipfp and not alwaysfp then
		player.CameraMode = Enum.CameraMode.Classic
		player.CameraMinZoomDistance = player.CameraMaxZoomDistance / 10
		wait()
		player.CameraMinZoomDistance = 0.5
	elseif alwaysfp then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	end
	gui.Enabled = false
	holdanim:Stop()
	shootanim:Stop()
	aiminanim:Stop()
	holdanim:Destroy()
	shootanim:Destroy()
	aiminanim:Destroy()



	--[[  Attenpt To Fix  ]]
	local magsize = 30
	local bullets = 30
	local reloading = false
	local maxrange = 100
	local fireRate = 0.1
	local reloadtime = 3
	--[[  End   ]]
	print("Logging: \\ 5") -- PrintLogging
end)

1 Like

Thanks for your reply.
I used your code and it is still having the same issue.

1 Like

I still believe it’s the loop that is causing the problem, but I never did test the code with the break to make sure, so It must be my fault why it did not work. Let me try some things with the code and see if it really is the loop.

1 Like

Alright Buddy! Found out it was not really the Loop. I noticed that the fire Event fired multiple times, and could not figure out why. So I did some research and came across this ← :+1:

2 Likes

Sorry for the late reply.
And how might I implement this?

1 Like

Basically, try not to put functions or loops :sweat_smile: in Equipped or Unequipped.
Here is your code, somethings may or may not need to be changed. I may of changed some stuff do to quick tests and such, like that break in the loop .

Fixed

repeat wait() until game:IsLoaded()

local gun = script.Parent
local player = script.Parent.Parent.Parent
local mouse = player:GetMouse()
task.wait(0.3)
local gui = script.Parent.M9Gui
local lp = game.Players.LocalPlayer
local txt = gui.FullFrame.TextLabel
local handle = script.Parent.Handle
local alwaysfp = false -- set this to true if you want to be in first person always
local equipfp = false -- set this true if you want to go into first person while holding the gun
local ret = false -- set this to false if you don't want a custom reticle and want to keep the default cursor
local reticle = "rbxassetid://2130621557" -- change this to the reticle you want
local reticleHM = "rbxassetid://2167171797" -- change this to the hit marker you want
local magsize = 30 -- how many bullets it can hold
local bullets = 30 -- how many bullets are in the mag
local reloading = false -- is the gun reloading or not
local maxrange = 100 -- max range the bullets can go
local fireRate = 0.1 -- fire rate in seconds (lower value means faster fire rate)
local reloadtime = 3 -- how long it takes to reload
local reloadkey = "r" -- what button to press to reload
local isFiring = false -- indicates whether the gun is firing or not
local holdanim1 = Instance.new("Animation")
local shootanim1 = Instance.new("Animation")
local aiminanim1 = Instance.new("Animation")
local reloadanim1 = Instance.new("Animation")
holdanim1.AnimationId = "rbxassetid://14089674542"
shootanim1.AnimationId = "rbxassetid://14089811993"
aiminanim1.AnimationId = "rbxassetid://14089811993"
reloadanim1.AnimationId = "rbxassetid://14213097033"
local holdanim = lp.Character.Humanoid:LoadAnimation(holdanim1)
local shootanim = lp.Character.Humanoid:LoadAnimation(shootanim1)
local aiminanim = lp.Character.Humanoid:LoadAnimation(aiminanim1)
local reloadanim = lp.Character.Humanoid:LoadAnimation(reloadanim1)
local canFire = false
local equipd = false
local UserInputService = game:GetService("UserInputService")
print("Logging: \\ 1") -- PrintLogging
gui.Parent = player.PlayerGui
txt.Text = bullets .. "/" .. magsize

if alwaysfp then
	player.CameraMode = Enum.CameraMode.LockFirstPerson
else
	player.CameraMode = Enum.CameraMode.Classic
end

local function bulletCos(hit, pos, flush, hole)
	if hole then
		--local bullethole = Instance.new("Part")
		--bullethole.FormFactor = "Custom"
		--bullethole.Size = Vector3.new(1, 1, 0.2)
		--bullethole.Anchored = true
		--bullethole.CanCollide = false
		--bullethole.Transparency = 1
		--local bulletholedecal = Instance.new("Decal", bullethole)
		--bulletholedecal.Face = "Front"
		--bulletholedecal.Texture = "http://www.roblox.com/asset/?id=2078626"
		--bullethole.Parent = game.Workspace
		--bullethole.CFrame = CFrame.new(pos, pos + flush)
		--game:GetService("Debris"):AddItem(bullethole, 3)
	end
	--[[ -- New Shoot Raycasting ]]
	local trail = Instance.new("Part")
	trail.BrickColor = BrickColor.new("Smoky grey")
	trail.FormFactor = "Custom"
	trail.Size = Vector3.new(0.01, 0.01, (gun.Handle.Front.Position - pos).Magnitude)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Transparency = 0.9
	trail.Material = Enum.Material.Neon
	trail.CFrame = CFrame.new(gun.Handle.Front.Position, pos) * CFrame.new(0, 0, -(gun.Handle.Front.Position - pos).Magnitude / 2)
	trail.Parent = game.Workspace
	game:GetService("Debris"):AddItem(trail, fireRate)

	--[[ -- Old Shoot Raycasting
	local trail = Instance.new("Part")
	trail.BrickColor = BrickColor.new("Fog")
	trail.FormFactor = "Custom"
	trail.Size = Vector3.new(0.01, 0.01, (gun.Handle.Front.Position - pos).Magnitude)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Transparency = 0.75
	trail.Material = Enum.Material.Plastic
	trail.CFrame = CFrame.new(gun.Handle.Front.Position, pos) * CFrame.new(0, 0, -(gun.Handle.Front.Position - pos).Magnitude / 2)
	trail.Parent = game.Workspace
	game:GetService("Debris"):AddItem(trail, fireRate)
	-]]
end


local function Fire()
		print("Logging: \\ 3") -- PrintLogging
		holdanim:Stop()
		shootanim:Play()

		if canFire then
			canFire = false -- disable firing temporarily
			while wait(fireRate) do
				if  isFiring and bullets > 0 and not reloading and equipd then
					bullets = bullets - 1
					handle.GunShot:Play()
					txt.Text = bullets .. "/" .. magsize
					local ray = Ray.new(gun.Handle.Front.Position, (mouse.Hit.p - gun.Handle.Front.Position).Unit * maxrange)
					local hit, position, flush = workspace:FindPartOnRay(ray, player.Character)
					if hit then
						local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
						if humanoid then

							bulletCos(hit, position, flush, false)
							script.Parent.BulletHit2:FireServer(humanoid)
							spawn(function()
								mouse.Icon = reticleHM
								wait(0.2)
								mouse.Icon = reticle
							end)
						else

							bulletCos(hit, position, flush, true)
						end
					end
					bulletCos(hit, position, flush, false)
					--wait(fireRate)
				else
					break
				end
			end	
			wait(fireRate) -- add additional delay before enabling firing again
			canFire = true -- enable firing
		end
	end
local function equipped()
	print("equipped")
	local FinalPlayer = player.Name
	game.Players:FindFirstChild(FinalPlayer).OTSView.Value = true




	holdanim:Play()
	
	if ret then
		mouse.Icon = reticle
	else
		mouse.Icon = ""
	end
	if equipfp then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		player.CameraMode = Enum.CameraMode.Classic
	end
	gui.Enabled = true

	canFire = true
end

local function uniquipped()
	print("unequipped")
	local FinalPlayer = player.Name
	game.Players:FindFirstChild(FinalPlayer).OTSView.Value = false


	if ret then
		mouse.Icon = ""
	end
	if equipfp and not alwaysfp then
		player.CameraMode = Enum.CameraMode.Classic
		player.CameraMinZoomDistance = player.CameraMaxZoomDistance / 10
		wait()
		player.CameraMinZoomDistance = 0.5
	elseif alwaysfp then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	end
	gui.Enabled = false
	holdanim:Stop()
	shootanim:Stop()
	aiminanim:Stop()
	holdanim:Destroy()
	shootanim:Destroy()
	aiminanim:Destroy()



	--[[  Attenpt To Fix  ]]
	local magsize = 30
	local bullets = 30
	local reloading = false
	local maxrange = 100
	local fireRate = 0.1
	local reloadtime = 3
	--[[  End   ]]
	print("Logging: \\ 5") -- PrintLogging
end

gun.Equipped:Connect(function()
	print("Logging: \\ 2") -- PrintLogging
	
	if not equipd then equipped() end 
	equipd = true
	
end)

mouse.KeyDown:Connect(function(key)
			if key == reloadkey and not reloading and equipd then
				reloading = true
				txt.Text = "Reloading"
				holdanim:Stop()
				shootanim:Stop()
				aiminanim:Stop()
				handle.Reload1:Play()
				reloadanim:Play()

				wait(reloadtime)
				bullets = magsize
				reloading = false
				txt.Text = bullets .. "/" .. magsize
				holdanim:Play()
			end
		end)
gun.Activated:Connect(function()
	if equipd then -- Check if the gun is equipped before firing
		isFiring = true
		Fire()
	end
end)

gun.Deactivated:Connect(function()
	if equipd then
		isFiring = false
		shootanim:Stop()
		holdanim:Play()
	end
end)

gun.Unequipped:Connect(function()

	-- Ev's Stuff
	print("Logging: \\ 4") -- PrintLogging
	if equipd then uniquipped() end
	equipd = false
end)

Also no worries for the late reply. I hope this helps and have a Blessed Day!

1 Like

It was close. I am getting an error though.

TextLabel is not a valid member of ScreenGui "Players.Ur_Dadeyyyyyy.Backpack.Mp5.M9Gui"  -  Client - Fire:9
1 Like

oh, lol. Sorry about that. I did mention that I may have of changed some coding.
Just replace gui.TextLabel with gui.FullFrame.TextLabel . I even had a Reminder to help me change it back(I forgot obviously). :grin: The code above will have this fixed.

1 Like

Works!
Thank you so much for your help!

2 Likes

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