How i can fix this script?

  1. I need fix this error
    Снимок экрана (13498)
    (text type of error Players.SerBogdan03.Backpack.AK47.GunLocal:500: attempt to index nil with ‘Play’ - Client - GunLocal:500)

this is script what need fixed:


local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local InputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local GunAssets = ReplicatedStorage:WaitForChild("GunAssets")
local GunRemotes = GunAssets:WaitForChild("Remotes")
local GunBindables = GunAssets:WaitForChild("Bindables")
local GunModules = GunAssets:WaitForChild("Modules")
local BulletVisualizer = require(GunModules:WaitForChild("MakeBullet"))

local CameraAssets = ReplicatedStorage:WaitForChild("CameraAssets")
local CameraBindables = CameraAssets:WaitForChild("Bindables")
local CameraSettings = require(GunModules:WaitForChild("CameraSettings"))

local MovementAssets = ReplicatedStorage:WaitForChild("MovementAssets")
local MovementSettings = MovementAssets:WaitForChild("Settings")




local Gun = script.Parent
local BodyAttach = Gun:WaitForChild("BodyAttach")
local ShootEvent = Gun:WaitForChild("ShootEvent")

local GunSettingsModule = Gun:WaitForChild("Settings")
local GunSettings = require(GunSettingsModule)
local GunFilter = require(GunModules:WaitForChild("GunFilterList"))

local GunStats = GunSettings.GunSettings
local BeamSettings = GunSettings.BeamSettings
local KeyBinds = GunSettings.KeyBinds
local MouseInfo = require(ReplicatedStorage:WaitForChild("MouseModule"))

HumanoidSpeed = 0

local CurrentSpread = GunStats.Spread
local CurrentAmmo = GunStats.Ammo
local Holstering = false
local AimFactor = 1

local AttachGui = function()
	spawn(function()
		GunGui = Player.PlayerGui.GunGui
		InputService.MouseIconEnabled = false
		local MouseFrame = GunGui.Mouse
		local TopLine = MouseFrame.TopLine
		local LeftLine = MouseFrame.LeftLine
		local RightLine = MouseFrame.RightLine
		local BottomLine = MouseFrame.BottomLine
		TopLine.BackgroundTransparency = 1
		BottomLine.BackgroundTransparency = 1
		LeftLine.BackgroundTransparency = 1
		RightLine.BackgroundTransparency = 1

		TweenService:Create(GunGui.Frame.WeaponName,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 0}):Play()
		TweenService:Create(GunGui.Frame.MaxAmmo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 0}):Play()
		TweenService:Create(GunGui.Frame.Ammo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 0}):Play()
		TweenService:Create(GunGui.Frame.AmmoIcon,GunSettings.GuiSettings.CursorFadeInfo,{ImageTransparency = .4}):Play()


		UpdateGui()

		GunGui.Enabled = true
		while RunService.RenderStepped:wait() do
			if Equipped then
				TweenService:Create(TopLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.5, .25 - CurrentSpread / 40)
				}):Play()
				TweenService:Create(BottomLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.5, .75 + CurrentSpread / 40)
				}):Play()
				TweenService:Create(LeftLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.25 - CurrentSpread / 40, .5)
				}):Play()
				TweenService:Create(RightLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.75 + CurrentSpread / 40, .5)
				}):Play()
			else

				if GunGui~=nil then
					local MouseFrame = GunGui.Mouse
					local TopLine = MouseFrame.TopLine
					local LeftLine = MouseFrame.LeftLine
					local RightLine = MouseFrame.RightLine
					local BottomLine = MouseFrame.BottomLine
					TweenService:Create(TopLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
					TweenService:Create(BottomLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
					TweenService:Create(LeftLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
					TweenService:Create(RightLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()

					TweenService:Create(GunGui.Frame.WeaponName,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 1}):Play()
					TweenService:Create(GunGui.Frame.MaxAmmo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 1}):Play()
					TweenService:Create(GunGui.Frame.Ammo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 1}):Play()
					TweenService:Create(GunGui.Frame.AmmoIcon,GunSettings.GuiSettings.CursorFadeInfo,{ImageTransparency = 1}):Play()

				end
				InputService.MouseIconEnabled = true
				wait(GunSettings.GuiSettings.CursorFadeInfo.Time)
				if GunGui~=nil and not Equipped and GunGui.Frame.WeaponName.TextTransparency>.6 then
					GunGui.Enabled = false
					InputService.MouseIconEnabled = true
				elseif GunGui.Frame.WeaponName.TextTransparency<.6 then
					InputService.MouseIconEnabled = false
				end
				break
			end
		end
	end)
end

AttachTool = function()
	Equipped = true
	AttachGui()

	CameraBindables.SetLockedMode:Fire(true)
	CameraBindables.SetCameraPosition:Fire(CameraSettings.GunOutPosition*Vector3.new(CameraSettings.Side,1,1),TweenInfo.new(.5))

	Character = Player.Character
	Humanoid = Character.Humanoid

	spawn(function()
		Player.Character.RightHand:WaitForChild("ToolGrip").Part1 = BodyAttach
	end)

	RecoilCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.RecoilAnim)
	ReloadCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.ReloadAnim)
	HolsterCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HolsterAnim)
	HoldCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HoldAnim)
	AimCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.AimAnim)
	GunRemotes.AttachTool:FireServer(BodyAttach)

	Holstering = false
	ToggleHolster()


	DiedEvent =
		Humanoid.Died:Connect(function()
			UnattachTool()
		end)

	SpeedLogEvent = 
		Humanoid.Running:Connect(function(S)
			HumanoidSpeed = S
		end)

end


UnattachTool = function()
	Equipped = false
	DiedEvent:Disconnect()
	SpeedLogEvent:Disconnect()

	CameraBindables.SetLockedMode:Fire(false)
	CameraBindables.SetCameraPosition:Fire(Vector3.new(0,2,0),TweenInfo.new(.5))
	CameraBindables.LockLookAround:Fire(false)
	GunBindables.ToggleWaist:Fire(false)

	HoldCurrent:Stop()
	RecoilCurrent:Stop()
	AimCurrent:Stop()

	Aiming = false
	Holstering = true
	ToggleHolster()



	GunRemotes.UnattachTool:FireServer()
end

Gun.Equipped:Connect(function()
	wait(0.1)
	AttachTool()
end)

Gun.Unequipped:Connect(function()
	wait(0.1)
	UnattachTool()
end)


local CanFire = true

Activated = false
Gun.Activated:Connect(function()
	Activated = true
	if GunStats.FiringMode == "Semi" or GunStats.FiringMode == "Single" then
		if Aiming and CanFire and Humanoid.WalkSpeed ~= 22 then
			Shoot()
			CanFire = false
			delay(GunStats.Cooldown, function()
				CanFire = true
			end)
		end
	end
end)

Gun.Deactivated:Connect(function()
	Activated = false
end)
RunService.Stepped:Connect(function()
	if CanFire and Activated and GunStats.FiringMode == "Auto" and  Humanoid.WalkSpeed ~= 22 then
		Shoot()
		CanFire = false
		delay(GunStats.Cooldown, function()
			CanFire = true
		end)
	end
end)


local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = GunFilter

local function CreateRandom()
	local Distance
	local SpreadRay = workspace:Raycast(Character.Head.CFrame.p, (MouseInfo.MousePosition - Character.Head.CFrame.p).unit * 1000, RayParams)
	if SpreadRay ~= nil then
		Distance = (SpreadRay.Position - Character.Head.CFrame.p).Magnitude
	else
		Distance = ((Character.Head.CFrame.p + (MouseInfo.MousePosition - Character.Head.CFrame.p).unit * 250) - Character.Head.CFrame.p).Magnitude
	end
	return math.clamp(math.random((1 + CurrentSpread) * -100, (1 + CurrentSpread) * 100) / 5000 * Distance, -40, 40)
end

function AddSpread()
	return Vector3.new(CreateRandom(), CreateRandom(), CreateRandom())
end

function ClampSpread(X)
	return math.clamp(X,
		(GunStats.Spread + HumanoidSpeed / 2 * GunStats.WalkSpeedSpreadFactor) / AimFactor,
		(GunStats.SpreadMax + HumanoidSpeed / 2 * GunStats.WalkSpeedSpreadFactor)/ AimFactor)
end

LastReductionTick = tick()
local ReduceSpreadWhileEquipped = function()
	SpreadReductionLoop = RunService.Stepped:Connect(function()
		if Equipped then


			if LastReductionTick + .025 < tick() then
				LastReductionTick = tick()
				CurrentSpread = ClampSpread(CurrentSpread - GunStats.SpreadRecovery)
			end	


		else
			SpreadReductionLoop:Disconnect()
		end
	end)
end
Gun.Equipped:Connect(ReduceSpreadWhileEquipped)


function IncreaseSpread()
	CurrentSpread = ClampSpread(CurrentSpread + GunStats.SpreadIncrease)
end

function UpdateGui()
	if GunGui ~= nil then
		GunGui.Frame.Ammo.Text = CurrentAmmo
		GunGui.Frame.MaxAmmo.Text = GunStats.Ammo
		GunGui.Frame.WeaponName.Text = Gun.Name
	end
end

function ToggleAim()
	if Equipped then
		if not Aiming then

			Holstering = true
			ToggleHolster()

			if GunGui~=nil then
				local MouseFrame = GunGui.Mouse
				local TopLine = MouseFrame.TopLine
				local LeftLine = MouseFrame.LeftLine
				local RightLine = MouseFrame.RightLine
				local BottomLine = MouseFrame.BottomLine
				TweenService:Create(TopLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()
				TweenService:Create(BottomLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()
				TweenService:Create(LeftLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()
				TweenService:Create(RightLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()

			end
			GunBindables.ToggleWaist:Fire(true)
			Aiming = true
			PlaySound(GunSettingsModule.Sounds.AimIn,BodyAttach)
			HoldCurrent:Play()
			AimFactor = GunStats.AimSpreadFactor
			TweenService:Create(Camera,TweenInfo.new(.5),{FieldOfView = GunStats.AimFov}):Play()
			CameraBindables.SetSensitivity:Fire(GunStats.AimMouseSensitivity)
		elseif Aiming then

			Holstering = false
			ToggleHolster()

			if GunGui~=nil then
				local MouseFrame = GunGui.Mouse
				local TopLine = MouseFrame.TopLine
				local LeftLine = MouseFrame.LeftLine
				local RightLine = MouseFrame.RightLine
				local BottomLine = MouseFrame.BottomLine
				TweenService:Create(TopLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
				TweenService:Create(BottomLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
				TweenService:Create(LeftLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
				TweenService:Create(RightLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()

			end
			GunBindables.ToggleWaist:Fire(false)
			Aiming = false
			PlaySound(GunSettingsModule.Sounds.AimOut,BodyAttach)
			HoldCurrent:Stop()
			AimFactor = 1
			CameraBindables.SetSensitivity:Fire(.45)
			TweenService:Create(Camera,TweenInfo.new(1),{FieldOfView = 70}):Play()
		end
	end
end

local function CheckIfHitHumanoid(PartHit)
	if PartHit~=nil and PartHit.Parent~=workspace then

		local CheckedParents = PartHit.Parent:FindFirstChild("Humanoid") or PartHit.Parent.Parent:FindFirstChild("Humanoid")

		if CheckedParents~=nil then
			return CheckedParents
		end

		for _,Player in pairs(Players:GetPlayers()) do
			if Player.Character and Player.Character:FindFirstChild("Humanoid") and PartHit:IsDescendantOf(Player.Character) then
				return Player.Character.Humanoid
			end
		end

	end
end

function Shoot()
	if CanFire and Equipped and CurrentAmmo > 0 and not Reloading and not Holstering then


		CurrentAmmo = CurrentAmmo - 1
		for i=1,GunStats.BulletNum do
			RayParams.FilterDescendantsInstances = GunFilter
			MouseInfo.IgnoreList = GunFilter
			local RayOrigin = Character.Head.CFrame.p
			local RayDirection = (MouseInfo.MousePosition - Character.Head.CFrame.p).unit * 1000 + AddSpread()
			IncreaseSpread()

			CameraBindables.Recoil:Fire(GunStats.RecoilAmount)

			RecoilCurrent:Play()

			local GunRay = workspace:Raycast(RayOrigin, RayDirection, RayParams)


			local EndPosition = GunRay and GunRay.Position or RayOrigin + RayDirection
			local HitPart
			local Normal
			if GunRay then
				HitPart = GunRay.Instance
				Normal = GunRay.Normal
			end
			UpdateGui()
			local IfHit = CheckIfHitHumanoid(HitPart)
			local HitHuman = false
			if IfHit~=nil then
				HitHuman = true
				local HitSound = GunSettingsModule.Sounds.HitSound:Clone()
				HitSound.Parent = game:GetService("SoundService")
				HitSound:Play()
				Debris:AddItem(HitSound,1)
				GunGui.Mouse.HitMark.ImageColor3 = Color3.new(1,1,1)
				GunGui.Mouse.HitMark.ImageTransparency = 0
				TweenService:Create(GunGui.Mouse.HitMark,TweenInfo.new(.7,Enum.EasingStyle.Linear),{ImageTransparency = 1}):Play()
				if HitPart.Name == "Head" then
					GunGui.Mouse.HitMark.ImageColor3 = Color3.new(1,0,0)
				end

			end



			if i==1 then
				BulletVisualizer:MakeBullet(GunSettingsModule, Gun.End, EndPosition, Normal, true,HitPart,HitHuman)
				ShootEvent:FireServer(EndPosition, HitPart,Normal,true)
			else
				BulletVisualizer:MakeBullet(GunSettingsModule, Gun.End, EndPosition, Normal, false,HitPart,HitHuman)
				ShootEvent:FireServer(EndPosition, HitPart,Normal,false)	
			end

		end

	elseif GunStats.AutoReload and not Reloading and Equipped and not Holstering and CurrentAmmo<1 then
		Reload()
	end
end

local ReloadSoundEvent = Gun:WaitForChild("ReloadSound")
local MagInSoundEvent = Gun:WaitForChild("MagInSound")
local MagOutSoundEvent = Gun:WaitForChild("MagOutSound")
local BoltPullSoundEvent = Gun:WaitForChild("BoltPullSound")

function PlaySound(Sound,Parent)
	local NewSound = Sound:Clone();NewSound.Parent = Parent
	NewSound:Play()
	game:GetService("Debris"):AddItem(NewSound,Sound.TimeLength)
end
-- johnpka washere!

function Reload()
	if not Reloading then
		Reloading = true
		CurrentAmmo = 0
		ReloadCurrent:Play()
		ReloadCurrent.Priority = Enum.AnimationPriority.Action

		local InEvent
		local OutEvent
		local BoltEvent

		if GunStats.ReloadType == "AnimationEvent" then
			InEvent = ReloadCurrent:GetMarkerReachedSignal("MagIn"):Connect(function()
				PlaySound(GunSettingsModule.Sounds.MagIn,BodyAttach)
				MagInSoundEvent:FireServer()
			end)

			OutEvent = ReloadCurrent:GetMarkerReachedSignal("MagOut"):Connect(function()
				PlaySound(GunSettingsModule.Sounds.MagOut,BodyAttach)
				MagOutSoundEvent:FireServer()
			end)

			BoltEvent = ReloadCurrent:GetMarkerReachedSignal("BoltPull"):Connect(function()
				PlaySound(GunSettingsModule.Sounds.BoltPull,BodyAttach)
				BoltPullSoundEvent:FireServer()
			end)
		else
			PlaySound(GunSettingsModule.Sounds.ReloadSound,BodyAttach)
			ReloadSoundEvent:FireServer()
		end

		if GunGui then
			GunGui.Frame.Ammo.Text = "RLD"
		end
		delay(GunStats.ReloadSpeed,function()
			CurrentAmmo = GunStats.Ammo
			UpdateGui()
			Reloading = false
			OutEvent:Disconnect();OutEvent = nil
			InEvent:Disconnect();InEvent = nil
			BoltEvent:Disconnect();BoltEvent = nil
		end)
	end

end

ReloadSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

MagInSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

MagOutSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

BoltPullSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

function ToggleHolster()
	if Holstering then
		if Equipped then
			CameraBindables.SetLockedMode:Fire(true)
			CameraBindables.LockLookAround:Fire(false)
			CameraBindables.SetCameraPosition:Fire(CameraSettings.GunOutPosition*Vector3.new(CameraSettings.Side,1,1),GunSettings.CameraSettings.AimTransitionInfo)
		end
		Holstering = false
		HolsterCurrent:Stop()
	elseif not Holstering then
		CameraBindables.SetLockedMode:Fire(false)
		CameraBindables.LockLookAround:Fire(true)
		CameraBindables.SetCameraPosition:Fire(Vector3.new(0,2,0),GunSettings.CameraSettings.AimTransitionInfo)
		Holstering  = true
		if Reloading then repeat wait() until not Reloading end
		if Equipped then
			HolsterCurrent:Play()
		end
	end
end

InputService.InputBegan:Connect(function(Input,Process)
	if Input.KeyCode == KeyBinds.ReloadKey and not Process and Equipped and CurrentAmmo<GunStats.Ammo then
		Reload()
	end

	if Input.KeyCode == KeyBinds.ChangeCameraSideKey and not Process and Equipped then
		CameraSettings.Side = CameraSettings.Side*-1
		if not Holstering and not Reloading then
			CameraBindables.SetCameraPosition:Fire(CameraSettings.GunOutPosition*Vector3.new(CameraSettings.Side,1,1),GunSettings.CameraSettings.AimTransitionInfo)
		end	
	end

	if Input.UserInputType == Enum.UserInputType.MouseButton2 and not Process and Equipped then
		if not Reloading and  Humanoid.WalkSpeed ~= 22 then
			Aiming = false
			ToggleAim()
		end
	end

	if Input.KeyCode == KeyBinds.SecondaryAimKey and not Process and Equipped then
		ToggleAim()
	end

end)

InputService.InputEnded:Connect(function(Input,Process)
	if Input.UserInputType == Enum.UserInputType.MouseButton2 then
		Aiming = true
		ToggleAim()
	end
end)

When this function happens and that statement:

function ToggleHolster()
elseif not Holstering then

this animation probably doesnt exist:

HolsterCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HolsterAnim)

Script output say that : Players.SerBogdan03.Backpack.AK47.GunLocal:492: attempt to index nil with ‘Stop’
Players.SerBogdan03.Backpack.AK47.GunLocal:500: attempt to index nil with ‘Play’
And yes,
Снимок экрана (13500)
this is AK47 anim’s

Same issue, that the previous one, you are trying to Stop() or Play() an animation that doesnt exist yet, that animation is suposedly in the module: GunSettingsModule.Animations.HolsterAnim, but your script cant find it. Thats why the error says index “nil” with Play or Stop, nil means you have nothing in that variable and you are trying to nil:Play() or nil:Stop()

Game has script - local Gun = script.Parent - its parent of script, next is local GunSettingsModule = Gun:WaitForChild(“Settings”) - this searching “Settings” ModuleScript for the gun
Снимок экрана (13502) so :confused:

If Settings is a module, you should do local GunSettingsModule = require(Gun:WaitForChild(“Settings”))


Right now, this making this error

Read the error, you didnt closed a line correctly, Expected a " ) " at line 33, its jumping to the next line “local”, what if you show those lines or the script?

Снимок экрана (13507)
:confused: A stop, i fix this


there other error

Look the first line

local GunSettingsModule = require(Gun:WaitForChild("Settings") -- < you are missing to close a ()
local GunSettingsModule = require(Gun:WaitForChild("Settings"))  -- < should be like this

The second line, its not needed, you dont need to require it twice, you already loaded the module

local GunSettings = require(GunSettingsModule) -- < this is not needed

You already have the module loaded in GunSettingsModule


It’s needed…

Dont send captures of the script, copy/paste script in the message, its hard to read an image
:sweat_smile:

1 Like

Okay,

local Gun = script.Parent
local BodyAttach = Gun:WaitForChild("BodyAttach")
local ShootEvent = Gun:WaitForChild("ShootEvent")

local GunSettingsModule = require(Gun:WaitForChild("Settings"))
local GunSettings = require(GunSettingsModule)
local GunFilter = require(GunModules:WaitForChild("GunFilterList"))

local GunStats = GunSettings.GunSettings
local BeamSettings = GunSettings.BeamSettings
local KeyBinds = GunSettings.KeyBinds
local MouseInfo = require(ReplicatedStorage:WaitForChild("MouseModule"))

And the rest of the script?

I think that line its reduntant, its the same as doing:

local GunSettingsModule = require(Gun:WaitForChild("Settings"))
local GunSettings = GunSettingsModule

It’s all script so idk :confused:


local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local InputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local GunAssets = ReplicatedStorage:WaitForChild("GunAssets")
local GunRemotes = GunAssets:WaitForChild("Remotes")
local GunBindables = GunAssets:WaitForChild("Bindables")
local GunModules = GunAssets:WaitForChild("Modules")
local BulletVisualizer = require(GunModules:WaitForChild("MakeBullet"))

local CameraAssets = ReplicatedStorage:WaitForChild("CameraAssets")
local CameraBindables = CameraAssets:WaitForChild("Bindables")
local CameraSettings = require(GunModules:WaitForChild("CameraSettings"))

local MovementAssets = ReplicatedStorage:WaitForChild("MovementAssets")
local MovementSettings = MovementAssets:WaitForChild("Settings")





local Gun = script.Parent
local BodyAttach = Gun:WaitForChild("BodyAttach")
local ShootEvent = Gun:WaitForChild("ShootEvent")

local GunSettingsModule = require(Gun:WaitForChild("Settings"))
local GunSettings = GunSettingsModule
local GunFilter = require(GunModules:WaitForChild("GunFilterList"))

local GunStats = GunSettings.GunSettings
local BeamSettings = GunSettings.BeamSettings
local KeyBinds = GunSettings.KeyBinds
local MouseInfo = require(ReplicatedStorage:WaitForChild("MouseModule"))

HumanoidSpeed = 0

local CurrentSpread = GunStats.Spread
local CurrentAmmo = GunStats.Ammo
local Holstering = false
local AimFactor = 1

local AttachGui = function()
	spawn(function()
		GunGui = Player.PlayerGui.GunGui
		InputService.MouseIconEnabled = false
		local MouseFrame = GunGui.Mouse
		local TopLine = MouseFrame.TopLine
		local LeftLine = MouseFrame.LeftLine
		local RightLine = MouseFrame.RightLine
		local BottomLine = MouseFrame.BottomLine
		TopLine.BackgroundTransparency = 1
		BottomLine.BackgroundTransparency = 1
		LeftLine.BackgroundTransparency = 1
		RightLine.BackgroundTransparency = 1

		TweenService:Create(GunGui.Frame.WeaponName,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 0}):Play()
		TweenService:Create(GunGui.Frame.MaxAmmo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 0}):Play()
		TweenService:Create(GunGui.Frame.Ammo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 0}):Play()
		TweenService:Create(GunGui.Frame.AmmoIcon,GunSettings.GuiSettings.CursorFadeInfo,{ImageTransparency = .4}):Play()


		UpdateGui()

		GunGui.Enabled = true
		while RunService.RenderStepped:wait() do
			if Equipped then
				TweenService:Create(TopLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.5, .25 - CurrentSpread / 40)
				}):Play()
				TweenService:Create(BottomLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.5, .75 + CurrentSpread / 40)
				}):Play()
				TweenService:Create(LeftLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.25 - CurrentSpread / 40, .5)
				}):Play()
				TweenService:Create(RightLine, TweenInfo.new(.1), {
					Position = UDim2.fromScale(.75 + CurrentSpread / 40, .5)
				}):Play()
			else

				if GunGui~=nil then
					local MouseFrame = GunGui.Mouse
					local TopLine = MouseFrame.TopLine
					local LeftLine = MouseFrame.LeftLine
					local RightLine = MouseFrame.RightLine
					local BottomLine = MouseFrame.BottomLine
					TweenService:Create(TopLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
					TweenService:Create(BottomLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
					TweenService:Create(LeftLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
					TweenService:Create(RightLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()

					TweenService:Create(GunGui.Frame.WeaponName,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 1}):Play()
					TweenService:Create(GunGui.Frame.MaxAmmo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 1}):Play()
					TweenService:Create(GunGui.Frame.Ammo,GunSettings.GuiSettings.CursorFadeInfo,{TextTransparency = 1}):Play()
					TweenService:Create(GunGui.Frame.AmmoIcon,GunSettings.GuiSettings.CursorFadeInfo,{ImageTransparency = 1}):Play()

				end
				InputService.MouseIconEnabled = true
				wait(GunSettings.GuiSettings.CursorFadeInfo.Time)
				if GunGui~=nil and not Equipped and GunGui.Frame.WeaponName.TextTransparency>.6 then
					GunGui.Enabled = false
					InputService.MouseIconEnabled = true
				elseif GunGui.Frame.WeaponName.TextTransparency<.6 then
					InputService.MouseIconEnabled = false
				end
				break
			end
		end
	end)
end

AttachTool = function()
	Equipped = true
	AttachGui()

	CameraBindables.SetLockedMode:Fire(true)
	CameraBindables.SetCameraPosition:Fire(CameraSettings.GunOutPosition*Vector3.new(CameraSettings.Side,1,1),TweenInfo.new(.5))

	Character = Player.Character
	Humanoid = Character.Humanoid

	spawn(function()
		Player.Character.RightHand:WaitForChild("ToolGrip").Part1 = BodyAttach
	end)

	RecoilCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.RecoilAnim)
	ReloadCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.ReloadAnim)
	HolsterCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HolsterAnim)
	HoldCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HoldAnim)
	AimCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.AimAnim)
	GunRemotes.AttachTool:FireServer(BodyAttach)

	Holstering = false
	ToggleHolster()


	DiedEvent =
		Humanoid.Died:Connect(function()
			UnattachTool()
		end)

	SpeedLogEvent = 
		Humanoid.Running:Connect(function(S)
			HumanoidSpeed = S
		end)

end


UnattachTool = function()
	Equipped = false
	DiedEvent:Disconnect()
	SpeedLogEvent:Disconnect()

	CameraBindables.SetLockedMode:Fire(false)
	CameraBindables.SetCameraPosition:Fire(Vector3.new(0,2,0),TweenInfo.new(.5))
	CameraBindables.LockLookAround:Fire(false)
	GunBindables.ToggleWaist:Fire(false)

	HoldCurrent:Stop()
	RecoilCurrent:Stop()
	AimCurrent:Stop()

	Aiming = false
	Holstering = true
	ToggleHolster()



	GunRemotes.UnattachTool:FireServer()
end

Gun.Equipped:Connect(function()
	wait(0.1)
	AttachTool()
end)

Gun.Unequipped:Connect(function()
	wait(0.1)
	UnattachTool()
end)


local CanFire = true

Activated = false
Gun.Activated:Connect(function()
	Activated = true
	if GunStats.FiringMode == "Semi" or GunStats.FiringMode == "Single" then
		if Aiming and CanFire and Humanoid.WalkSpeed ~= 22 then
			Shoot()
			CanFire = false
			delay(GunStats.Cooldown, function()
				CanFire = true
			end)
		end
	end
end)

Gun.Deactivated:Connect(function()
	Activated = false
end)
RunService.Stepped:Connect(function()
	if CanFire and Activated and GunStats.FiringMode == "Auto" and  Humanoid.WalkSpeed ~= 22 then
		Shoot()
		CanFire = false
		delay(GunStats.Cooldown, function()
			CanFire = true
		end)
	end
end)


local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = GunFilter

local function CreateRandom()
	local Distance
	local SpreadRay = workspace:Raycast(Character.Head.CFrame.p, (MouseInfo.MousePosition - Character.Head.CFrame.p).unit * 1000, RayParams)
	if SpreadRay ~= nil then
		Distance = (SpreadRay.Position - Character.Head.CFrame.p).Magnitude
	else
		Distance = ((Character.Head.CFrame.p + (MouseInfo.MousePosition - Character.Head.CFrame.p).unit * 250) - Character.Head.CFrame.p).Magnitude
	end
	return math.clamp(math.random((1 + CurrentSpread) * -100, (1 + CurrentSpread) * 100) / 5000 * Distance, -40, 40)
end

function AddSpread()
	return Vector3.new(CreateRandom(), CreateRandom(), CreateRandom())
end

function ClampSpread(X)
	return math.clamp(X,
		(GunStats.Spread + HumanoidSpeed / 2 * GunStats.WalkSpeedSpreadFactor) / AimFactor,
		(GunStats.SpreadMax + HumanoidSpeed / 2 * GunStats.WalkSpeedSpreadFactor)/ AimFactor)
end

LastReductionTick = tick()
local ReduceSpreadWhileEquipped = function()
	SpreadReductionLoop = RunService.Stepped:Connect(function()
		if Equipped then


			if LastReductionTick + .025 < tick() then
				LastReductionTick = tick()
				CurrentSpread = ClampSpread(CurrentSpread - GunStats.SpreadRecovery)
			end	


		else
			SpreadReductionLoop:Disconnect()
		end
	end)
end
Gun.Equipped:Connect(ReduceSpreadWhileEquipped)


function IncreaseSpread()
	CurrentSpread = ClampSpread(CurrentSpread + GunStats.SpreadIncrease)
end

function UpdateGui()
	if GunGui ~= nil then
		GunGui.Frame.Ammo.Text = CurrentAmmo
		GunGui.Frame.MaxAmmo.Text = GunStats.Ammo
		GunGui.Frame.WeaponName.Text = Gun.Name
	end
end

function ToggleAim()
	if Equipped then
		if not Aiming then

			Holstering = true
			ToggleHolster()

			if GunGui~=nil then
				local MouseFrame = GunGui.Mouse
				local TopLine = MouseFrame.TopLine
				local LeftLine = MouseFrame.LeftLine
				local RightLine = MouseFrame.RightLine
				local BottomLine = MouseFrame.BottomLine
				TweenService:Create(TopLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()
				TweenService:Create(BottomLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()
				TweenService:Create(LeftLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()
				TweenService:Create(RightLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 0}):Play()

			end
			GunBindables.ToggleWaist:Fire(true)
			Aiming = true
			PlaySound(GunSettingsModule.Sounds.AimIn,BodyAttach)
			HoldCurrent:Play()
			AimFactor = GunStats.AimSpreadFactor
			TweenService:Create(Camera,TweenInfo.new(.5),{FieldOfView = GunStats.AimFov}):Play()
			CameraBindables.SetSensitivity:Fire(GunStats.AimMouseSensitivity)
		elseif Aiming then

			Holstering = false
			ToggleHolster()

			if GunGui~=nil then
				local MouseFrame = GunGui.Mouse
				local TopLine = MouseFrame.TopLine
				local LeftLine = MouseFrame.LeftLine
				local RightLine = MouseFrame.RightLine
				local BottomLine = MouseFrame.BottomLine
				TweenService:Create(TopLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
				TweenService:Create(BottomLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
				TweenService:Create(LeftLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()
				TweenService:Create(RightLine,GunSettings.GuiSettings.CursorFadeInfo,{BackgroundTransparency = 1}):Play()

			end
			GunBindables.ToggleWaist:Fire(false)
			Aiming = false
			PlaySound(GunSettingsModule.Sounds.AimOut,BodyAttach)
			HoldCurrent:Stop()
			AimFactor = 1
			CameraBindables.SetSensitivity:Fire(.45)
			TweenService:Create(Camera,TweenInfo.new(1),{FieldOfView = 70}):Play()
		end
	end
end

local function CheckIfHitHumanoid(PartHit)
	if PartHit~=nil and PartHit.Parent~=workspace then

		local CheckedParents = PartHit.Parent:FindFirstChild("Humanoid") or PartHit.Parent.Parent:FindFirstChild("Humanoid")

		if CheckedParents~=nil then
			return CheckedParents
		end

		for _,Player in pairs(Players:GetPlayers()) do
			if Player.Character and Player.Character:FindFirstChild("Humanoid") and PartHit:IsDescendantOf(Player.Character) then
				return Player.Character.Humanoid
			end
		end

	end
end

function Shoot()
	if CanFire and Equipped and CurrentAmmo > 0 and not Reloading and not Holstering then


		CurrentAmmo = CurrentAmmo - 1
		for i=1,GunStats.BulletNum do
			RayParams.FilterDescendantsInstances = GunFilter
			MouseInfo.IgnoreList = GunFilter
			local RayOrigin = Character.Head.CFrame.p
			local RayDirection = (MouseInfo.MousePosition - Character.Head.CFrame.p).unit * 1000 + AddSpread()
			IncreaseSpread()

			CameraBindables.Recoil:Fire(GunStats.RecoilAmount)

			RecoilCurrent:Play()

			local GunRay = workspace:Raycast(RayOrigin, RayDirection, RayParams)


			local EndPosition = GunRay and GunRay.Position or RayOrigin + RayDirection
			local HitPart
			local Normal
			if GunRay then
				HitPart = GunRay.Instance
				Normal = GunRay.Normal
			end
			UpdateGui()
			local IfHit = CheckIfHitHumanoid(HitPart)
			local HitHuman = false
			if IfHit~=nil then
				HitHuman = true
				local HitSound = GunSettingsModule.Sounds.HitSound:Clone()
				HitSound.Parent = game:GetService("SoundService")
				HitSound:Play()
				Debris:AddItem(HitSound,1)
				GunGui.Mouse.HitMark.ImageColor3 = Color3.new(1,1,1)
				GunGui.Mouse.HitMark.ImageTransparency = 0
				TweenService:Create(GunGui.Mouse.HitMark,TweenInfo.new(.7,Enum.EasingStyle.Linear),{ImageTransparency = 1}):Play()
				if HitPart.Name == "Head" then
					GunGui.Mouse.HitMark.ImageColor3 = Color3.new(1,0,0)
				end

			end



			if i==1 then
				BulletVisualizer:MakeBullet(GunSettingsModule, Gun.End, EndPosition, Normal, true,HitPart,HitHuman)
				ShootEvent:FireServer(EndPosition, HitPart,Normal,true)
			else
				BulletVisualizer:MakeBullet(GunSettingsModule, Gun.End, EndPosition, Normal, false,HitPart,HitHuman)
				ShootEvent:FireServer(EndPosition, HitPart,Normal,false)	
			end

		end

	elseif GunStats.AutoReload and not Reloading and Equipped and not Holstering and CurrentAmmo<1 then
		Reload()
	end
end

local ReloadSoundEvent = Gun:WaitForChild("ReloadSound")
local MagInSoundEvent = Gun:WaitForChild("MagInSound")
local MagOutSoundEvent = Gun:WaitForChild("MagOutSound")
local BoltPullSoundEvent = Gun:WaitForChild("BoltPullSound")

function PlaySound(Sound,Parent)
	local NewSound = Sound:Clone();NewSound.Parent = Parent
	NewSound:Play()
	game:GetService("Debris"):AddItem(NewSound,Sound.TimeLength)
end
-- johnpka washere!

function Reload()
	if not Reloading then
		Reloading = true
		CurrentAmmo = 0
		ReloadCurrent:Play()
		ReloadCurrent.Priority = Enum.AnimationPriority.Action

		local InEvent
		local OutEvent
		local BoltEvent

		if GunStats.ReloadType == "AnimationEvent" then
			InEvent = ReloadCurrent:GetMarkerReachedSignal("MagIn"):Connect(function()
				PlaySound(GunSettingsModule.Sounds.MagIn,BodyAttach)
				MagInSoundEvent:FireServer()
			end)

			OutEvent = ReloadCurrent:GetMarkerReachedSignal("MagOut"):Connect(function()
				PlaySound(GunSettingsModule.Sounds.MagOut,BodyAttach)
				MagOutSoundEvent:FireServer()
			end)

			BoltEvent = ReloadCurrent:GetMarkerReachedSignal("BoltPull"):Connect(function()
				PlaySound(GunSettingsModule.Sounds.BoltPull,BodyAttach)
				BoltPullSoundEvent:FireServer()
			end)
		else
			PlaySound(GunSettingsModule.Sounds.ReloadSound,BodyAttach)
			ReloadSoundEvent:FireServer()
		end

		if GunGui then
			GunGui.Frame.Ammo.Text = "RLD"
		end
		delay(GunStats.ReloadSpeed,function()
			CurrentAmmo = GunStats.Ammo
			UpdateGui()
			Reloading = false
			OutEvent:Disconnect();OutEvent = nil
			InEvent:Disconnect();InEvent = nil
			BoltEvent:Disconnect();BoltEvent = nil
		end)
	end

end

ReloadSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

MagInSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

MagOutSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

BoltPullSoundEvent.OnClientEvent:Connect(function(SoundInstance)
	SoundInstance.Volume = 0
end)

function ToggleHolster()
	if Holstering then
		if Equipped then
			CameraBindables.SetLockedMode:Fire(true)
			CameraBindables.LockLookAround:Fire(false)
			CameraBindables.SetCameraPosition:Fire(CameraSettings.GunOutPosition*Vector3.new(CameraSettings.Side,1,1),GunSettings.CameraSettings.AimTransitionInfo)
		end
		Holstering = false
		HolsterCurrent:Stop()
	elseif not Holstering then
		CameraBindables.SetLockedMode:Fire(false)
		CameraBindables.LockLookAround:Fire(true)
		CameraBindables.SetCameraPosition:Fire(Vector3.new(0,2,0),GunSettings.CameraSettings.AimTransitionInfo)
		Holstering  = true
		if Reloading then repeat wait() until not Reloading end
		if Equipped then
			HolsterCurrent:Play()
		end
	end
end

InputService.InputBegan:Connect(function(Input,Process)
	if Input.KeyCode == KeyBinds.ReloadKey and not Process and Equipped and CurrentAmmo<GunStats.Ammo then
		Reload()
	end

	if Input.KeyCode == KeyBinds.ChangeCameraSideKey and not Process and Equipped then
		CameraSettings.Side = CameraSettings.Side*-1
		if not Holstering and not Reloading then
			CameraBindables.SetCameraPosition:Fire(CameraSettings.GunOutPosition*Vector3.new(CameraSettings.Side,1,1),GunSettings.CameraSettings.AimTransitionInfo)
		end	
	end

	if Input.UserInputType == Enum.UserInputType.MouseButton2 and not Process and Equipped then
		if not Reloading and  Humanoid.WalkSpeed ~= 22 then
			Aiming = false
			ToggleAim()
		end
	end

	if Input.KeyCode == KeyBinds.SecondaryAimKey and not Process and Equipped then
		ToggleAim()
	end

end)

InputService.InputEnded:Connect(function(Input,Process)
	if Input.UserInputType == Enum.UserInputType.MouseButton2 then
		Aiming = true
		ToggleAim()
	end
end)

Try removing the wait above the AttachTool call?
image

This line is missing at the top btw:
local SpreadReductionLoop

if i do this game say this : 15:41:12.615 Players.SerBogdan03.Backpack.AK47.GunLocal:133: attempt to index nil with 'RecoilAnim' - Client - GunLocal:133
and this is part

RecoilCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.RecoilAnim)
	ReloadCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.ReloadAnim)
	HolsterCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HolsterAnim)
	HoldCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.HoldAnim)
	AimCurrent = Humanoid:LoadAnimation(GunSettingsModule.Animations.AimAnim)
	GunRemotes.AttachTool:FireServer(BodyAttach)