My script is failing to index a mouse event

Hello. I’m making an automatic gun, but, when I try to index MouseButton1Down event and MouseButton1Up event of a mouse. Here’s my code:

local tool = script.Parent;

local remoteEvent = script.Parent.Remote;

local shooting = false;
	
remoteEvent.OnServerEvent:Connect(function(player)
	
	local mouse = player:GetMouse()
	
	mouse.Button1Down:Connect(function()
		
		shooting = true
	end)
	
	mouse.Button1Up:Connect(function()
		
		shooting = false
	end)
	
	while wait(0.09) do
		
		if shooting and not script.Parent.IsReloading.Value and not script.Parent.IsRunning.Value then
			
			local bullet = game.ReplicatedStorage.Bullet;
			
			local bulletClone = bullet:Clone();
			
			bulletClone.Parent = workspace
			
			bulletClone.Position = player.Character.RightLowerArm.Position + player.Character.RightLowerArm.CFrame.LookVector * 2
			
			local bodyVelocity = Instance.new("BodyVelocity")
			
			bodyVelocity.Parent = bulletClone
				
			local unitRange = 150;
			
			local unitRay = (mouse.Hit.p - bulletClone.Position).Unit * tonumber(unitRange)
			
			bodyVelocity.Velocity = unitRay
		end
	end
end)

Is there anything I did wrong?

1 Like

I’m assuming this is in a server script. The :GetMouse() event only works in a local script

1 Like

Oh, it is a ServerScript. Do you suggest me maybe send the mouse from the Client to the ServerScript?

That’s one way to do it, though sending that much data repetitively to the server with a remote event will lead to delays and latency.

The best way to create a gun is that have the bullet effect and physics display on the client, its properties (like travel direction and range) fired to the server to check whether hit box collisions are logical (this is called a sanity check), and fired to the rest of the clients to produce a bullet effect on their own network.

1 Like

Oh. So to not show the bullet on the Server but on the Client?

1 Like

Show the bullet on the client (because showing the bullet on the server will lag the game, and firing remote events consistently to the server will cause delay), but fire the bullet properties to the server so that the server can fire it to the rest of the clients, and each client will produce the same bullet properties so that it’s lag-free on the server.

Detect hits on the client and have the server check to make sure they are reasonable (to make sure exploiters don’t have an advantage)

1 Like

Understood. Thank you!
And by the way, I made a writing mistake that I noticed only when you were typing and you may have been distracted to see I fixed it.

1 Like