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

Attempt to index field * (a function value)?

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

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.

1 comment

0

Could you go into a little more detail on what you mean by You can change all of those to Block.BlockName = BlockType?

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?

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