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

My script to get the color and material not working, what is wrong???

So, I made a script to get a two values, then make a part using those. I have a StringValue called current, which would keep a value like "Brick" and a Color3Value called CurrentColor which would contain an RGB value, like "248, 0, 9" and when I try to reference them in a script to make a block out of them, it errors. My script is:

local rs = game.ReplicatedStorage
local event = rs.GrassPlace
local current = workspace.Current
local color = workspace.CurrentColor

function place(plr,target,blocktype)
    local newBlock = Instance.new("Part")
    newBlock.Size = Vector3.new(4,4,4)
    newBlock.Anchored = true
    newBlock.Material = current
    newBlock.BrickColor = color.Value
    newBlock.Name = current.Value
    newBlock.CFrame = CFrame.new(target.Position) + CFrame.new(0,4,0)
    newBlock.Parent = workspace
end

event.OnServerEvent:Connect(place)

I also have a script inside a GUI that changes the "Current" value. If it helps, that script is:

local current = workspace.Current
local colorValue = workspace.CurrentColor

script.Parent.MouseButton1Click:Connect(function()
	current.Value = "Brick"
	colorValue.Value = script.Color.Value
end)

Please help me!!!

8 comments

0

All you really need to worry about are lines 10 and 11.

0

or lines 3 and 4

0

IDK lol

0

Enum.Material.current?

0

change line 10 to newBlock.Material = current

0

Also you didn't input a brick color for line 11

0

newBlock.Color = Color3.FromRGB(color.Value) -- try that

0

Workspace.BlockPlacement:13: attempt to call field 'FromRGB' (a nil value)

2 answers

First, if you want to use a variable as a key for any table, you use table[variable], or Enum.Material[current] in this case.

Second, to make a BrickColor from a Color3 value, you can do BrickColor.new(color) or insert the RGB values separately BrickColor.new(color.r, color.g, color.b)

Fixed code:

local rs = game.ReplicatedStorage
local event = rs.GrassPlace
local current = workspace.Current.Value
local color = workspace.CurrentColor.Value

function place(plr,target,blocktype)
    local newBlock = Instance.new("Part", workspace)
    newBlock.Size = Vector3.new(4,4,4)
    newBlock.Anchored = true
    newBlock.Material = Enum.Material[current]
    newBlock.BrickColor = BrickColor.new(color)
    newBlock.Name = current
    newBlock.CFrame = CFrame.new(target.Position) + Vector3.new(0,4,0)
end

event.OnServerEvent:Connect(place)

5 comments

0

Your code gave me the error "Is not a valid EnumItem."

0

Also, current is not a table, as I mentioned it holds a value like "Brick" or "Grass"

0

current is not the table, Enum.Material is the table, current is the variable that is the key for the table, so it uses table[key], and the error "Is not a valid EnumItem means your value is not a valid material, I tested it using "Brick" and it works, if you misspelled something as "Brickk" it will show that error, you can test it in the command line like this: print(Enum.Material["Brick"])

0

That works, I didn't mis spell it, it still says "Is not a valid EnumItem."

0

What material are you trying to use? What value do you have for current?

The reason it because your doing BrickColor.color() not color.Value Your also adding a Vector3 to a CFrame; fixed that too

local rs = game.ReplicatedStorage
local event = rs.GrassPlace
local current = workspace.Current
local color = workspace.CurrentColor

function place(plr,target,blocktype)
	local newBlock = Instance.new("Part")
	newBlock.Size = Vector3.new(4,4,4)
	newBlock.Anchored = true
	newBlock.Material = current.Value
	newBlock.Color3= color.Value
	newBlock.Name = current.Value
	newBlock.CFrame = CFrame.new(target.Position) + CFrame.new(0,4,0)
	newBlock.Parent = workspace
end

event.OnServerEvent:Connect(place)

5 comments

0

Ok, It still says "Is not a valid EnumItem." I am updating the question with one of my scripts.

0

Can you post the console error?

0

Invalid value "" for enum Material

0

Now it gives me

0

Invalid value for enum Material

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