Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my targeting function work?

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.