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

How to fix this Mouse.Target error?

Asked by 9 years ago

Ok so I been modifying this Door System that let's users click the door and teleport to the other door in the same model but when I click the door it comes up with this error attempt to index field 'Target' (a nil value) How can I get it to be fixed?

Here's the section the error appears on.

-- Main Values

local Mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer

-- Check whether the mouse has been moved and check whether a description needs to be shown.
Mouse.Move:connect(function()
    local target = Mouse.Target
    if target then
        if not player.Character or not player.Character:FindFirstChild("Torso") then
            return
        end
        local distance = (player.Character.Torso.Position - target.Position).magnitude
        local message -- What to say
        if target.Parent:FindFirstChild('Head') and target.Parent:FindFirstChild('Torso') and distance < 25 then
            message = target.Parent.Name;
        elseif target.Parent:FindFirstChild('Item') and distance < 15 then
            message = target.Parent.Item.Value;
        end
        if message then
            if not player.PlayerGui:FindFirstChild("Description") then
                local description = game.ReplicatedStorage["Door System"].Graphics.Description:Clone()
                description.Parent = player.PlayerGui
            end
            local description = game.ReplicatedStorage["Door System"].Graphics.Description:Clone()
            description.Information.Text = message
        else
            if player.PlayerGui:FindFirstChild("Description") then
                player.PlayerGui.Description:Destroy()
            end
        end
    end
end)


-- Check whether the player has decided to click something I.E a door or a torch.
Mouse.Button1Down:connect(function()
    local Target = Mouse.Target
    if Target then
        if Target.Parent.Name == 'FirstDoor' then
            repeat wait() until game.Players.LocalPlayer.Character
            local Magnitude = (game.Players.LocalPlayer.Character.Torso.Position - Target.Position).magnitude
            if Magnitude < 10 then
                game.ReplicatedStorage['Door System'].Events.WalkThroughDoor:FireServer(Target.Parent, Target.Parent.Parent:FindFirstChild('SecondDoor'))               
            end 
        elseif Target.Parent.Name == 'SecondDoor' then
            repeat wait() until game.Players.LocalPlayer.Character
            local Magnitude = (game.Players.LocalPlayer.Character.Torso.Position - Target.Position).magnitude
            if Magnitude < 10 then  
                game.ReplicatedStorage['Door System'].Events.WalkThroughDoor:FireServer(Target.Parent, Target.Parent.Parent:FindFirstChild('FirstDoor'))                    
            end                         
        end
    end
end)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The issue is as the error says -- Mouse.Target is nil. How can this be? It's because time passed (from wait()) after your check that it's not nil.


The simple solution is to save the original one in a variable. Since you say it very many times, that makes sense anyways. You should do the same thing with the local player:

local player = game.Players.LocalPlayer

Mouse.Move:connect(function()
    local target = Mouse.Target
    if target then
        if target.Parent:FindFirstChild('Head') and target.Parent:FindFirstChild('Torso') then
            repeat wait() until player.Character
            local Magnitude = (player.Character.Torso.Position - target.Position).magnitude
            if not player.PlayerGui:FindFirstChild('Description') and Magnitude < 25 then
                local Description = game.ReplicatedStorage['Door System'].Graphics['Description']:Clone()
                Description.Information.Text = target.Parent.Name
                Description.Parent = player.PlayerGui
            end
        elseif target.Parent:FindFirstChild('Item') then
            repeat wait() until player.Character
            local Magnitude = (player.Character.Torso.Position - target.Position).magnitude  --Error right here
            if not player.PlayerGui:FindFirstChild('Description') and Magnitude < 15 then         
                local Description = game.ReplicatedStorage['Door System'].Graphics['Description']:Clone()
                Description.Information.Text = target.Parent.Item.Value
                Description.Parent = player.PlayerGui
            end
        else
            if player.PlayerGui:FindFirstChild('Description') then
                player.PlayerGui.Description:remove()
            end
        end
    end
end)

ROBLOX prefers :Remove() over :remove() and it prefers :Destroy() over :Remove().

There's no reason to use ['Description'], you can just say .Description.

You repeat your computation of Magnitude -- you can do it just once before the if instead. It's also a bad name for distance -- magnitude says how big something is -- you should say what if you want your code to make sense!

You should also be careful that the Character actually has a .Torso. You might as well just use an if instead of waiting until it's there.

You also repeat a lot of the code for showing the message. It's probably simpler to just make a message variable and act on its contents after.

local player = game.Players.LocalPlayer

Mouse.Move:connect(function()
    local target = Mouse.Target
    if target then
        if not player.Character or not player.Character:FindFirstChild("Torso") then
            return
        end
        local distance = (player.Character.Torso.Position - target.Position).magnitude
        local message -- What to say
        if target.Parent:FindFirstChild('Head') and target.Parent:FindFirstChild('Torso') and distance < 25 then
            message = target.Parent.Name;
        elseif target.Parent:FindFirstChild('Item') and distance < 15 then
            message = target.Parent.Item.Value;
        end
        if message then
            if not player.PlayerGui:FindFirstChild("Description") then
                local description = game.Replicated["Door System"].Graphics.Description:Clone()
                description.Parent = player.PlayerGui
            end
            description.Information.Text = message
        else
            if player.PlayerGui:FindFirstChild("Description") then
                player.PlayerGui.Description:Destroy()
            end
        end
    end
end)

This is much simpler -- less redundant -- and hopefully easier to work with.

0
Thx for fixing it but now the next part of the script does not work at all... I edited my post with the full code this time. Anciteify 70 — 9y
0
it also shows label not the name of the part it's on. Anciteify 70 — 9y
Ad

Answer this question