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

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
Umm it didn't work, I added a new part of the script to my question. NickIsANuke 217 — 4y
0
My bad, check my answer again. royaltoe 5144 — 4y
0
It errors on line 11. " Workspace.BlockPlacement:11: attempt to index upvalue 'blockValue' (a nil value)" NickIsANuke 217 — 4y
Ad

Answer this question