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

Attempt to concatenate field "x" (a nil value) help??

Asked by 6 years ago

I have the dictionary with jobs inside a modulescript in repl. storage:

local Jobs = {}

Jobs.AvailableJobs = {}
Jobs.AvailableJobs["PizzaChef"] = {Name = "Pizza Chef"}, {Photo = "1934128599"}, {ReqLvl = 1}
Jobs.AvailableJobs["StoreClerk"] = {Name = "Store Clerk"}, {Photo = "1934127830"}, {ReqLvl = 1}
Jobs.AvailableJobs["PoliceOfficer"] = {Name = "Law Enforcement"}, {Photo = "1934126760"}, {ReqLvl = 2}
Jobs.AvailableJobs["Medic"] = {Name = "Doctor"}, {Photo = "1934126267"}, {ReqLvl = 3}

return Jobs

And whenever I try to set an image labels image to the photo ID I get the error:

19:49:50.856 - Players.radusavin366.PlayerGui.HUD.MainHUD.Buttons.JobPopup.LocalScript:65: attempt to concatenate field 'Photo' (a nil value)

(attempt to set the image:)

        local JobImage = Instance.new("ImageLabel", JobBox)
        JobImage.BackgroundTransparency = 1
        JobImage.Position = UDim2.new(0.248,0,0.057,0)
        JobImage.Size = UDim2.new(0,100,0,100)
        JobImage.Image = "rbxassetid://" .. Jobs.AvailableJobs[id].Photo

Full script (local, inside PlayerGui, triggered by it's parent that is a gui button):

local exists = false
local Jobs = require(game.ReplicatedStorage.Jobs)

---constants

local x = 0
local y = 1
local z = 0

script.Parent.MouseButton1Click:Connect(function()
    local JobPanel
    if not exists then
    exists = true
    JobPanel = Instance.new("Frame", script.Parent.Parent.Parent.Parent)
    JobPanel.BackgroundTransparency = 1
    JobPanel.Name = "Job Panel"
    JobPanel.AnchorPoint = Vector2.new(0.5, 0.5)
    JobPanel.Position = UDim2.new(0.5,0,0.5,0)
    JobPanel.Size = UDim2.new(0,800,0,450)
    local Title = Instance.new("TextLabel", JobPanel)
    Title.BackgroundColor3 = Color3.new(255,255,255)
    Title.Size = UDim2.new(0,750,0,30)
    Title.Text = "Job Menu"
    Title.TextScaled = true
    local Close = Instance.new("TextButton", JobPanel)
    Close.BackgroundColor3 = Color3.new(255,53,53)
    Close.Position = UDim2.new(0.938,0,0,0)
    Close.Size = UDim2.new(0,50,0,30)
    Close.Text = "Close"
    Close.MouseButton1Click:Connect(function()
        JobPanel:Destroy()
        exists = false
        x = 0
        y = 1
        z = 0
    end)
    local JobFrame = Instance.new("ScrollingFrame", JobPanel)
    JobFrame.Position = UDim2.new(0,0,0.067,0)
    JobFrame.Size = UDim2.new(0,600,0,420)
    JobFrame.ScrollBarThickness = 3
    local JobInfo = Instance.new("Frame", JobPanel)
    JobInfo.Position = UDim2.new(0.75,0,0.067,0)
    JobInfo.Size = UDim2.new(0,200,0,420)
    local selectAJob = Instance.new("TextLabel", JobInfo)
    selectAJob.Position = UDim2.new(-0.002,0,0,0)
    selectAJob.Size = UDim2.new(0,200,0,50)
    selectAJob.Text = "Select a job."
    for id, v in pairs(Jobs.AvailableJobs) do
        local o = 200
        local u = 210
        local JobBox = Instance.new("Frame", JobFrame)
        JobBox.BackgroundTransparency = 1
        JobBox.Size = UDim2.new(0,200,0,210)
        JobBox.Position = UDim2.new(0,o * x,0, u * z)
        x = x + 1
        y = y + 1
        if y == 3 then
            y = 1
            z = z + 1
        end
        local JobImage = Instance.new("ImageLabel", JobBox)
        JobImage.BackgroundTransparency = 1
        JobImage.Position = UDim2.new(0.248,0,0.057,0)
        JobImage.Size = UDim2.new(0,100,0,100)
        JobImage.Image = "rbxassetid://" .. Jobs.AvailableJobs[id].Photo
        local JobName = Instance.new("TextButton", JobBox)
        JobName.Position = UDim2.new(0.1,0,0.748,0)
        JobName.Size = UDim2.new(0,160,0,40)
        JobName.Text = Jobs.AvailableJobs[id].Name
        local Lvl = Instance.new("TextLabel", JobBox)
        Lvl.Size = UDim2.new(0,160,0,26)
        Lvl.Position = UDim2.new(0.1,0,0.626,0)
        Lvl.Text = "Required level: " .. Jobs.AvailableJobs[id].ReqLvl
    end
    end
end)

Help please!

0
The IDs you are using a "Decal"s. They should be "IMAGEs". Insert the decal using the toolbox and copy the texture ID from the properties tab. BlueTaslem 18071 — 6y
0
Oh, thanks. radusavin366 617 — 6y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
6 years ago
Edited 6 years ago

The error says exactly what's wrong:

You're attempting to concatenate (..) the field Jobs.AvailableJobs[id].Photo, but it's nil, which can't be concatenated with anything.

Why is Jobs.AvailableJobs[id].Photo nil?

Supposedly, you set it here:

Jobs.AvailableJobs["PoliceOfficer"] = {Name = "Law Enforcement"}, {Photo = "1934126760"}, {ReqLvl = 2}

However, this is not a correct initialization!

You have three separate comma-separated objects on the right hand side, but just a single variable on the left hand size. That means the second two are just thrown away.

What you probably meant is this:

Jobs.AvailableJobs["PoliceOfficer"] = {
    Name = "Law Enforcement",
    Photo = "1934126760",
    ReqLvl = 2,
}

assigning just a single dictionary with the three fields.

0
Thank you! This seemed to fix it, but the image doesn't display for some reason? No output message. radusavin366 617 — 6y
Ad

Answer this question