Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index local 'mouse' (a nil value)?

Hello, I am working on making a gun.

I've been getting the error "Attempt to index local 'mouse' (a nil value)" when I click

I got the mouse through a remote event so I could use a script for everyone to see the bullet. Filtering is disabled.

Local script:

local player = game:GetService("Players").LocalPlayer --Gets the player
local mouse = player:GetMouse() //Gets the mouse

mouse.Button1Down:Connect(function()
	script.Parent.GunRemote:FireServer(mouse, player) --Fires the server with variables "mouse" and "player"
	print("Fired") -- Debug
end)

Script:

script.Parent.Parent.Gun.GunRemote.OnServerEvent:connect(function(plr, mouse, player) 
local gun = script.Parent
local debounce = false 
print("2") --debug

		if debounce == true then return end
		debounce = true
		local ray = Ray.new(gun.CFrame.p, (mouse.Hit.p - gun.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		local distance = (gun.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(gun.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		beam.Touched:connect(function(who)
			local explosion = Instance.new("Explosion")
			explosion.Position = mouse.Hit.p
			explosion.BlastPressure = 1000
			explosion.BlastRadius = 1.5
			explosion.Parent = workspace
			
		end)
		
		
		wait(1)
		debounce = false
		print("debounce") --debug	
		
	end)

Any help is apreciated.

2 comments

0

On line 2 in the local script, you are using "//" instead of "--".

0

Not sure if thats the problem but try typing "Connect" with capital C in line 23. Try maybe using "local mouse = plr.GetMouse (). I am not the best in Lua but hope I helped somehow.

1 answer

✓ Accepted answer
script.Parent.GunRemote:FireServer(mouse, player) --wrong order + can't pass mouse variable because the mouse variable itself won't work in a global script 

but you can pass the mouse's hit CFrame and mouse's hit position. so use them instead.

mousehit = game.Players.LocalPlayer:GetMouse().Hit
mousehitp = game.Players.LocalPlayer:GetMouse().Hit.p

Final version of this line:

script.Parent.GunRemote:FireServer(player, mousehit)  --(mousehit) or (mousehitp)

8 comments

0

Could you explain some more?

0

Your order was wrong, player must be always first.

0

"//Gets the mouse" might also cause the script to error, use "--" to ignore code.

0

Yeah, I keep thinking it's // instead of --

0

Did it work out for you?

0

Also why aren't you using filtering enabled without it your script will error because the global script have no idea what mouse is.

0

Also i forgot to mention, mouse would be nil always, you can pass only the mouse's hit and mouse's hit position.

0

I editted my answer, it should work for you now.

This discussion is preserved as it was. New answers, comments, and votes are closed.