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

Attempt to index field * (a function value)?

Asked by
gunter5 17
4 years ago

Error message I receive: Workspace.WorldGenerator:6: (also lines 7-9) attempt to index field 'Bedrock' (a function value)

Here's the code albeit a bit inefficient. but you get the idea.

I'm attempting to create all my block data inside of a module script which I can retrieve from other scripts (such as my terrain generator) simply for having it all in a nice convenient layout.

On a side note, I know that returning a value makes it 'read-only' but I can't really think of how else I could make this happen.

-- Server script

local Blocks = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("BlockStatistics"))
local worldwidth = 50
local worldheight = 8

function PrepareGrid()
    print(Blocks.Bedrock.Name)
--  Blocks.Bedrock.Position = Vector3.new(-(worldwidth/2),-worldheight, -(worldwidth/2)) --laying bottom layer for Bedrock
--  Blocks.Stone.Position = Blocks.Bedrock.Position
--  Blocks.Dirt.Position = Blocks.Bedrock.Position

( I decided to leave only the relevant parts of the script which is tossing the error codes)

-- Module Script

local Blocks = {}

local BlockSize = Vector3.new(3,3,3) --Size of blocks in-game.
local BlockShape = "Block"

Blocks.Air = function()
    local ItemID = 0 -- this is an attempt to support commands for spawning in items
    local Size = BlockSize
    local Shape = BlockShape
end

Blocks.Stone = function()
    local ItemID = 1
    local Size = BlockSize
    local Shape = BlockShape
    local Stone = game:GetService("ServerStorage"):WaitForChild("Blocks"):WaitForChild("Terrain"):WaitForChild("Stone")
    return Stone
end

Blocks.Dirt = function()
    local ItemID = 3
    local Size = BlockSize
    local Shape = BlockShape
    local Dirt = game:GetService("ServerStorage"):WaitForChild("Blocks"):WaitForChild("Terrain"):WaitForChild("Dirt")
    return Dirt
end

Blocks.Bedrock = function()
    local ItemID = 7
    local Size = BlockSize
    local Shape = BlockShape
    local Bedrock = game:GetService("ServerStorage"):WaitForChild("Blocks"):WaitForChild("Terrain"):WaitForChild("Bedrock")
    return Bedrock
end

return Blocks 

2 answers

Log in to vote
0
Answered by 4 years ago

You have multiple occurrences of this for other block types like Air and Stone. Since you are complaining about Block.Bedrock.Name throwing an exception, I'll base this more on bedrock.

Blocks.Bedrock points to a function, and you're doing this for everything else, like Stone.

"On a side note, I know that returning a value makes it 'read-only' but I can't really think of how else I could make this happen."

This is only true for immutable values, like strings, numbers, etc. You can't change them.

Since you are returning a reference to these Bedrock, Stone, etc. objects, you just need to call the function, and then index.

print(Blocks.Bedrock().Name)

However what is this other code doing?

Blocks.Bedrock = function()
    local ItemID = 7
    local Size = BlockSize
    local Shape = BlockShape
    local Bedrock = game:GetService("ServerStorage"):WaitForChild("Blocks"):WaitForChild("Terrain"):WaitForChild("Bedrock")
    return Bedrock
end

You're not actually doing anything with these variables.

You can change all of those to Block.BlockName = BlockType.

And then Blocks.Bedrock.Name will work without a call.

0
Could you go into a little more detail on what you mean by You can change all of those to `Block.BlockName = BlockType`? gunter5 17 — 4y
Ad
Log in to vote
0
Answered by
gunter5 17
4 years ago
local Blocks = {}

local BlockSize = Vector3.new(3,3,3) --Size of blocks in-game.
local BlockShape = "Block"

Blocks.Bedrock = function()
    local ItemID = 7
    local Block = Instance.new("Part")
    Block.Size = BlockSize
    Block.Shape = BlockShape
end

return Blocks

My bad, I was making this as soon as I woke up so had a couple stupid mistakes. I'm wanting this game to be fully scripted (just for practice).

Hows this look?

Answer this question