Attempt to index nil with 'GetMouse'

Hey!

I am trying to make a scope in my gun system. I am using a Module to make a settings thing, and I am trying to get the mouse, to make the scope.

Here is my Module Script:

local mouse = game.Players.LocalPlayer:GetMouse()

local module = {
	----Main
	['WeaponName'] = 'AK47';
	['FireKeyCode'] = Enum.UserInputType.MouseButton1;
	['ShellType'] = 1; --1 rifle, 2 shotgun, 3 pistol
	['Auto'] = true;
	['Ammo'] = 30; --30 default
	['Mag'] = 30; --30 default
	['Bullets'] = 1; --1 default
	['Spread'] = 1; --1 default
	['BulletVelocity'] = 1000; --1000 default
	['FireRate'] = 0.15; --0.15 default
	['Damage'] = 21; --36 default
	['HeadShotDamage'] = 67; --60 default
	['MaxRange'] = 2000; --2000 default
	['MouseIcon'] = 'http://www.roblox.com/asset/?id=5681283745';
	['ReloadTime'] = 2.1; --2.1 default
	['Recoil'] = 10; --10 default
	['EquipTime'] = 0.5; --0.5 default
	----Mods
	['SafeMode'] = false;
	----Keys
	['ReloadKey'] = Enum.KeyCode.R;
	['SafeModeKey'] = Enum.KeyCode.V;
}

return module

This is the line of the error:

local mouse = game.Players.LocalPlayer:GetMouse()

Let me know how to fix this. Thank you!

Sincerely,

papahetfan

ModuleScripts are server-side and there is no such thing as a “LocalPlayer” for the server. You must define the player and get their mouse in a LocalScript.

Correction: ModuleScripts can be local, but it’s still ill-advised to get the player through the ModuleScript. Get it from the client and then use it with the ModuleScript’s methods.

2 Likes