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

How to get a variable's value, then clone it???

I wrote a script to place a block:

local rs = game.ReplicatedStorage
local event = rs.GrassPlace
local current = workspace.CurrentBlock
local TemplateFolder = game.ServerStorage.TemplateBlocks
local blockValue = game.ServerStorage:FindFirstChild(current.Value)

current.Value = "Brick"

function place(plr,target,blocktype)
	if current then
		local blockInFolder = TemplateFolder[blockValue.Value]
		
		if blockInFolder then
			local newBlock = TemplateFolder[blockValue.Value]:Clone()
		end

		newBlock.Parent = workspace.Blocks
    	newBlock.Size = Vector3.new(4,4,4)
    	newBlock.Anchored = true
    	newBlock.Name = current.Value
    	newBlock.CFrame = CFrame.new(target.Position) + Vector3.new(0,4,0)
    	newBlock.Parent = workspace
	end
end

event.OnServerEvent:Connect(place)

It says newBlock isn't defined, what is wrong??

1 answer

If blockValue is the name of your block inside TemplateFolder that you want to clone, you need to find the block and then clone it. That is what the following code does:

local blockInFolder = TemplateFolder[blockValue.Value]

if(blockInFolder)then local newBlock = TemplateFolder[blockValue.Value]:Clone() end

Replace line 11 with the following code above.

[EDIT]: My bad, make sure you have the rest of the code changing newBlock in the same scope(area) as where you made the new block variable.

	local rs = game.ReplicatedStorage
local event = rs.GrassPlace
local current = workspace.CurrentBlock
local TemplateFolder = game.ServerStorage.TemplateBlocks
local blockValue = game.ServerStorage:FindFirstChild(current.Value)

current.Value = "Brick"

function place(plr,target,blocktype)
    if current then
        local blockInFolder = TemplateFolder[blockValue.Value]

        if blockInFolder then
           	local newBlock = TemplateFolder[blockValue.Value]:Clone()
	 	newBlock.Parent = workspace.Blocks
	        newBlock.Size = Vector3.new(4,4,4)
	        newBlock.Anchored = true
	        newBlock.Name = current.Value
	        newBlock.CFrame = CFrame.new(target.Position) + Vector3.new(0,4,0)
	        newBlock.Parent = workspace
        end

       
    end
end

event.OnServerEvent:Connect(place)

3 comments

0

Umm it didn't work, I added a new part of the script to my question.

0

My bad, check my answer again.

0

It errors on line 11. " Workspace.BlockPlacement:11: attempt to index upvalue 'blockValue' (a nil value)"

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