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)
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.
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
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.
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
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)
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