Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Whenever I click mouse anywhere on sky this comes up: attempt to index field 'Target' (a nil value)?

Asked by 8 years ago
local Tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()



local ToolEnabled = true

Tool.Activated:connect( function()
    if not ToolEnabled then
        return 
    end

    ToolEnabled = false

    if mouse.Target.Position.magnitude == _G.ve1.magnitude and _G.humanoid1.Health < 101 then
        _G.humanoid1.Health = _G.humanoid1.Health + 6
    end

ve1 is a vector3

This is not the full script but the error is in the "if statement"

3 answers

Log in to vote
0
Answered by 8 years ago

A sky is NOT a part, that's why.

Ad
Log in to vote
2
Answered by 8 years ago

The reason this errors is because mouse.Target is specifically meant for targeting parts. The skybox isn't a part, and because of that, it has no position or magnitude, which causes an error. There is no target, so it is a nil value. Try adding a conditional that checks for if there's a target in the first place, and that might fix your issue.

Hope this helped.

0
Yup. Although, the mouse can target things other than parts, such as terrain. OldPalHappy 1477 — 8y
Log in to vote
-1
Answered by 8 years ago
Edited 8 years ago

You need to check whether your target exists!

if mouse.Target ~= nil then
    if mouse.Target.Position.magnitude == _G.ve1.magnitude and _G.humanoid1.Health < 101 then
        _G.humanoid1.Health = _G.humanoid1.Health + 6
    end
end

The following statement checks whether the mouse's target is an object. To avoid errors simply put your code in-between the statement below.

if mouse.Target ~= nil then end

Example:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.Move:connect(function()
    if mouse.Target ~= nil then 
        print(mouse.Target.Name)
    end
end)

Answer this question