How i can fix this script?

This code is a terrible mess. It’s filled with undefined variables.

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 Equipped = false
local SpreadReductionLoop = 1
local Reloading = false
local Aiming = false
local CanFire = true

local ReloadCurrent =

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


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






local Character = game.Players.LocalPlayer.Character

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 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

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

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

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)



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)


1 Like

Whre you got that code, I dont think its properly set, idk how looks the module, its children, if you have the anims in there, you own them?

I am not scripter from good typing something :confused:

Animation’s created by me, yea i can recreate it for test

If you created those anims, and you got that system from somewhere else, what you did to place your animations in that module that is looking for the anims? could you show the module that reference the anims?

  1. Gun AK47 Settings Module Script
local Settings = {
	
	BeamSettings = {
		Size = 0.05;
		
		Color = ColorSequence.new(Color3.new(1, 0.909804, 0.447059));
		
		Transparency = NumberSequence.new{
			NumberSequenceKeypoint.new(0,0)	,
			NumberSequenceKeypoint.new(.1,0),
			NumberSequenceKeypoint.new(.2,0),
			NumberSequenceKeypoint.new(.8,0),
			NumberSequenceKeypoint.new(1,1)
		};
		
		MuzzleFlash = "Default";
		
		
	};
	
	GunSettings = {
		Cooldown = .11;
		FiringMode = "Auto";
		Damage = 17;
		Ammo = 30;
		ReloadSpeed = 3;
		ReloadType = "AnimationEvent";
		AutoReload = true;
		Spread = 0;
		SpreadMax = 0;
		AimSpreadFactor = 2;
		SpreadRecovery = .6;
		SpreadIncrease = 6;
		WalkSpeedSpreadFactor = 1;
		
		BulletNum = 1;
		
		AimFov = 60;
		--^ Default Fov is 70^--
		
		AimMouseSensitivity = .35;
		--^ Default Sensitivity is .45^--
		
		RecoilAmount = Vector2.new(15,15);
		--^ Horizontal Recoil, Vertical Recoil^--
		
	};
	
	KeyBinds = {
		ReloadKey = Enum.KeyCode.R;
		HolsterKey = Enum.KeyCode.F;
		ChangeCameraSideKey = Enum.KeyCode.T;
		SecondaryAimKey = Enum.KeyCode.V;
		
		
	};
	
	GuiSettings = {
		CursorFadeInfo = TweenInfo.new(.5,Enum.EasingStyle.Linear)
		
	};
	
	CameraSettings = {
		AimTransitionInfo = TweenInfo.new(.2,Enum.EasingStyle.Linear)
		
	}
	
}




return Settings

and all other Scripts its from other - guns, has module script named “Settings” and the “GunLocal, GunServer” scripts.
GunServer Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GunAssets = ReplicatedStorage:WaitForChild("GunAssets")
local GunModules = GunAssets:WaitForChild("Modules")
local GunRemotes = GunAssets:WaitForChild("Remotes")



local Gun = script.Parent
local ShootEvent = Gun:WaitForChild("ShootEvent")
local ReloadSoundEvent = Gun:WaitForChild("ReloadSound")
local MagInSoundEvent = Gun:WaitForChild("MagInSound")
local MagOutSoundEvent = Gun:WaitForChild("MagOutSound")
local BoltPullSoundEvent = Gun:WaitForChild("BoltPullSound")
local EndPoint = Gun:WaitForChild("End")
local BodyAttach = Gun:WaitForChild("BodyAttach")

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

local function CheckIfHitHumanoid(PartHit)
	if 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

local VisualizeBulletsEvent = GunRemotes:WaitForChild("VisualizeBulletsToClient")


local function VisualizeBulletsToOthers(Player,EndPosition,Normal,DoEffect,PartHit,HitHuman)
	for _,Player2 in pairs(Players:GetPlayers()) do
		if Player~=Player2 then
			VisualizeBulletsEvent:FireClient(Player2,EndPoint,EndPosition,GunSettingsModule,Normal,DoEffect,PartHit,HitHuman)
		end
	end
end

ShootEvent.OnServerEvent:Connect(function(Player,EndPosition,PartHit,Normal,DoEffect)
	if Player and EndPosition then
		local HitHuman = false
			if PartHit~=nil then
		local CheckedInstance = CheckIfHitHumanoid(PartHit)
		
			if CheckedInstance~=nil then
				HitHuman = true
					if PartHit.Name == "Head" then
					CheckedInstance:TakeDamage(GunSettings.GunSettings.Damage*2)
				else
					CheckedInstance:TakeDamage(GunSettings.GunSettings.Damage)
				end
			end
				end
		--
			VisualizeBulletsToOthers(Player,EndPosition,Normal,DoEffect,PartHit,HitHuman)
	end
end)

local function ReloadSoundF(Player,Sound,Parent)
	local ReloadSound = Sound:Clone();ReloadSound.Parent = Parent
	ReloadSound:Play()
	ReloadSoundEvent:FireClient(Player,ReloadSound)
	game:GetService("Debris"):AddItem(ReloadSound,Sound.TimeLength)
end

local function MagInSoundF(Player,Sound,Parent)
	local MagInSound = Sound:Clone();MagInSound.Parent = Parent
	MagInSound:Play()
	MagInSoundEvent:FireClient(Player,MagInSound)
	game:GetService("Debris"):AddItem(MagInSound,Sound.TimeLength)
end

local function MagOutSoundF(Player,Sound,Parent)
	local MagOutSound = Sound:Clone();MagOutSound.Parent = Parent
	MagOutSound:Play()
	MagOutSoundEvent:FireClient(Player,MagOutSound)
	game:GetService("Debris"):AddItem(MagOutSound,Sound.TimeLength)
end


local function BoltPullSoundF(Player,Sound,Parent)
	local BoltPullSound = Sound:Clone();BoltPullSound.Parent = Parent
	BoltPullSound:Play()
	BoltPullSoundEvent:FireClient(Player,BoltPullSound)
	game:GetService("Debris"):AddItem(BoltPullSound,Sound.TimeLength)
end




ReloadSoundEvent.OnServerEvent:Connect(function(Player)
	ReloadSoundF(Player,GunSettingsModule.Sounds.ReloadSound,BodyAttach)
end)

MagInSoundEvent.OnServerEvent:Connect(function(Player)
	MagInSoundF(Player,GunSettingsModule.Sounds.MagIn,BodyAttach)
end)

MagOutSoundEvent.OnServerEvent:Connect(function(Player)
	MagOutSoundF(Player,GunSettingsModule.Sounds.MagOut,BodyAttach)
end)

BoltPullSoundEvent.OnServerEvent:Connect(function(Player)
	BoltPullSoundF(Player,GunSettingsModule.Sounds.BoltPull,BodyAttach)
end)

And here GunMainServer script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")

local GunAssets = ReplicatedStorage:WaitForChild("GunAssets")
local Remotes = GunAssets:WaitForChild("Remotes")

local AttachRemote = Remotes:WaitForChild("AttachTool")
local UnattachRemote = Remotes:WaitForChild("UnattachTool")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local RightHand = Player.Character:WaitForChild("RightHand")
		local Motor = script.ToolGrip:Clone()
		
		
	end)
end)

AttachRemote.OnServerEvent:Connect(function(Player,Location)
		if Player.Character then
	local Character = Player.Character
		local RightHand = Character:WaitForChild("RightHand")
		
		if RightHand:FindFirstChild("ToolGrip")==nil then
			local Motor = script:WaitForChild("ToolGrip"):Clone();Motor.Parent = RightHand
			Motor.Part0 = RightHand
		end
		RightHand.ToolGrip.Part1 = Location
	
	end
end)

UnattachRemote.OnServerEvent:Connect(function(Player)
	if Player.Character then
		local Character = Player.Character
		local RightHand = Character:WaitForChild("RightHand")
		pcall(function()
			RightHand.ToolGrip.Part1 = nil
		end)	
	end
end)

local RemotesFolder = GunAssets:WaitForChild("Remotes")
local TorsoMoveRemote = RemotesFolder:WaitForChild("WaistRotate")


local TweenService = game:GetService("TweenService")
TorsoMoveRemote.OnServerEvent:Connect(function(Player,CamDirection,TorsoOffset,Toggle)
	for _,OPlayer in pairs(Players:GetPlayers()) do
		if OPlayer~=Player then
			TorsoMoveRemote:FireClient(OPlayer,Player,CamDirection,TorsoOffset,Toggle)
		end
	end
end)