My gun script bugs out when i shoot in the air

This is the error that comes up when I shoot the sky or above me. After it does not shoot anymore than bugs out.
[01:54:38.949 - Players.slinky22.Backpack.Gun.Guninfo:26: attempt to index field ‘Target’ (a nil value)]

script.Parent.Activated:Connect(function()
	if Ammo>0 and not Reloading then
		if debounce then
		debounce = false
		Ammo=Ammo-1
		game.ReplicatedStorage.SoundEvent:FireServer()
		game.ReplicatedStorage.RayCastEvent:FireServer(script.Parent.Hole.CFrame.Position, Mouse.Hit.Position)
		if Mouse.Target.Parent:FindFirstChild("Zombie") then
			script.Parent.Damage:FireServer(Mouse.Target.Parent, 30)	
		end
		wait(0.5)
		debounce = true
	end
	
	elseif Reloading == false then
		Reload()
	end
end)
1 Like

I assume when you’re using Mouse you’re referring to the player mouse which you get from Player:GetMouse().
When your mouse isn’t over a part, Mouse.Target will be nil, so you should check if Mouse.Target is nil before indexing it.

Sidenote-

Do you have a remote which can be fired by the client to damage anyone? This seems very exploitable.

4 Likes

I’m curious, what’s the point of using 3 different remote events? Also, do you have any checks on the server?

1 Like

Target will set to nil when mouse is pointing on sky

script.Parent.Activated:Connect(function()
	if Ammo>0 and not Reloading then
		if debounce then
		debounce = false
		Ammo=Ammo-1
		game.ReplicatedStorage.SoundEvent:FireServer()
		game.ReplicatedStorage.RayCastEvent:FireServer(script.Parent.Hole.CFrame.Position, Mouse.Hit.Position)
		if Mouse.Target and Mouse.Target.Parent 
and Mouse.Target.Parent:FindFirstChild("Zombie") then
			script.Parent.Damage:FireServer(Mouse.Target.Parent, 30)	
		end
		wait(0.5)
		debounce = true
	end
	
	elseif Reloading == false then
		Reload()
	end
end)

I tried without them but the sounds from guns wont be heard from other players and also the raycast beam cant be seen without this

I don’t mean using forgoing the other effects, but I mean packing everything which the server uses under one RemoteEvent.
Basically,

Remote1:FireServer()
Remote2:FireServer()
Remote3:FireServer()

vs

Remote:FireServer()