How do I stop my gun system from breaking whenever you shoot at the skybox?

Whenever I shoot at the sky I get this error:
attempt to index nil with ‘Parent’
I have no clue how to fix this and any help would be appreciated

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent

Tool.Activated:Connect(function()
	if Mouse.Target.Parent then
		Tool.FireGun:FireServer(Mouse.Target)
	end
end)

1 Like

So when you fire the gun, your script is assuming that it is an object or a part. So on the line that has the error just expect it to sometimes be nil.

1 Like

trying to sent whatevergame.rbxl but fails because it’s not instance

1 Like

Show the server script for reviewing.

2 Likes

Mouse.Target is nil if you aim at the skybox. Then if Mouse.Target is nil, and you do Mouse.Target.Parent, it’s the equivalent of doing (nil).Parent, thus why you are getting that error.
To fix it, simply add a check for Mouse.Target, before checking for Mouse.Target.Parent

	if Mouse.Target and Mouse.Target.Parent then
		Tool.FireGun:FireServer(Mouse.Target)
	end

You also don’t need to check for Mouse.Target.Parent, as it is guaranteed to have a parent if the mouse is over it

1 Like

Mouse.Target only exists when the cursor is placed over an instance. The skyboy would not be included in that. A property exists on the Mouse object called Mouse.Hit which represents a CFrame of the mouse’s hit position (or alternatively, 1000 studs into the skybox when not hovering over a part).

You could fire the remote event with both the Mouse.Target and Mouse.Hit if you need to also fire a bullet or whatever into the skybox.

1 Like

You’re asking if there is the Mouse.Target.Parent without asking if there is a Mouse.Target.

1 Like

I’d recommend doing the script in a slightly different way:

  • Instead of using Mouse.Target, it’s better to use Mouse.Hit.Position
  • Then use a Raycast to validate if the ray hits a Player/Object
  • if the Raycast returns nil, then make it so the hitPos means the furthest point of the raycast
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local mouse = player:GetMouse()

local weapon = script.Parent

local range = 1000

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true

weapon.Activated:Connect(function()
	local mousePos = mouse.Hit.Position
	
	local origin = char.Head.Position
	local direction = CFrame.new(char.Head.CFrame.Position, mousePos).LookVector

	local rayResult = workspace:Raycast(char.Head.Position, direction * range, raycastParams)
	
	local hitHumanoid, hitPart, hitPos

	if rayResult then
		hitPart = rayResult.Instance
		hitPos = rayResult.Position


		local maybeChar = hitPart and hitPart:FindFirstAncestorOfClass("Model")

		if maybeChar and maybeChar:FindFirstChild("Humanoid") then
			hitHumanoid = maybeChar:FindFirstChild('Humanoid')
		end
	end
	
	if not hitPos then
		hitPos = origin + direction * range
	end
	
	print(hitHumanoid, hitPart, hitPos)
end)

I made a very quickly made script without sending anything to the server to damage the enemy etc etc

*if you do need any more assistance for anything else let me know

1 Like

Ok somehow after 8 comments nobody has been helpful

Here you go:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent

Tool.Activated:Connect(function()
	if Mouse and Mouse.Target then
		Tool.FireGun:FireServer(Mouse.Target)
	end
end)

You can copy and paste this directly

1 Like

There is no need to check if mouse exists

Using checks like that would make it so the gun doesn’t fire at all if the mouse is hovering over the skybox

This is consistent with the original functionality. The key here is breaking as in a full error. OP did not specify anything else

He’s firing a remote event called "FireGun", I presume he’s doing a gun system, sooo for the sake of not having a second problem to a solution, just do it the right way the first shot

1 Like

Well, I agree there isn’t enough information or the complete error to say for sure.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.