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

Why won't my targeting function work?

Asked by 8 years ago

I am trying to create a tool that will write inventions into a crafting tool for my game. It gives me this error: "10:57:20.902 - Players.Player.Backpack.Inventor.LocalScript:16: attempt to index field 'Target' (a nil value)" It is in a local script that is in a hopperbin.

local T = script.Parent
local P = game.Players.LocalPlayer
local M = P:GetMouse()
local I = {}
local i = {"hi", "hi", "hi"}
function CreateGui()
    T.ScreenGui.Parent = P.PlayerGui
end

function RemoveGui()
    P.PlayerGui.ScreenGui.Parent = T
end
function Target()
    if M.Target ~= nil then 
    P.ScreenGui.Target.Text = "Target: " .. M.Target
    end
end
T.Selected:connect(CreateGui)
T.Deselected:connect(RemoveGui)
M.Target.Changed:connect(Target)

1 answer

Log in to vote
0
Answered by 8 years ago

I may see your problem. I spent a few days working out one of these problems with my inventory system. Basically, even though you sated "if m.Target ~= nil", it'll say that the target is nil later in the script if you call it. I fixed it by making m.Target a variable at the beginning of the script. Because mouse.Target could technically change later in the script, using it as a variable will keep it consistent, if I explained that well.

function Target()
local target = M.Target
if target ~= nil then
P.ScreenGui.Target.Text = "Target: "..target
end
end

That should work, maybe? If it doesn't, msg me on roblox and I'll try to figure this out for you.

Ad

Answer this question