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

Inserting a brick with Filtering Enabled?

Asked by
NotSoNorm 777 Moderation Voter
8 years ago

So I have a local script fire a RemoteEvent and the firing and stuff works but the reciving script doesn't

It works in test but not in play mode, In play mode it gives the error: 'Workspace.BlockInserter:8: attempt to index a nil value' Line 8

NormalParts = {"Slab","Wall","Plate"}
local event = script.BlockInsertation

event.OnServerEvent:connect(function(player, name)
local chil = game.Workspace[name]:GetChildren()
    for i = 1, #chil do
        if chil[i].Name == NormalParts[1] or NormalParts[2] or NormalParts[3] then
            local clone = game.workspace[name]:FindFirstChild(NormalParts[1] or NormalParts[2] or NormalParts[3]):Clone()
            clone.Parent = game.Workspace
            clone.Transparency = 0
            print("Not brokenennn")
            break
        else
            local con = script.Parent.MoneyDisplay.ClonedErrors:GetChildren()
            if #con < 1 then
                local cloneerror =  script.Parent.MoneyDisplay.ErrorDisplay:Clone()
                cloneerror.Parent = script.Parent.MoneyDisplay.ClonedErrors
                cloneerror.Description.Text = "Undefined part value"
                cloneerror:TweenPosition(UDim2.new(0.78, 0,0.92, 0),"In","Linear",.2,false)
                wait(1.5)
                cloneerror:TweenPosition(UDim2.new(0.78, 0,1.1, 0),"Out","Linear",.2,false)
                wait(1)
                cloneerror:Destroy()
            end
        end 
    end
end)

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

or does not work the way you are using it.

or does not magically find the the value that will work. It just returns true if any of the things you give it are truthy.

Since "Slab", "Wall", and "Plate" are not false, or will be useless when used on just these.

See this question from just today.


That means your check for the name must be explicit:

if chil[i].Name == NormalParts[1] or chil[i].Name == NormalParts[2] or chil[i].Name == NormalParts[3] then

You should really consider using better variable names. Also, workspace is much shorter than game.Workspace and a little safer:

local children = workspace[name]:GetChildren()
for _, child in pairs(children) do
    if child.Name == ...... etc

Similarly, you can't use or in findfirstchild, because you will just get :FindFirstChild("Slab"), which may not exist.

You could be explicit:

local a = workspace[name]:FindFirstChild( NormalParts[1] )
local b = workspace[name]:FindFirstChild( NormalParts[2] )
local c = workspace[name]:FindFirstChild( NormalParts[3] )
local clone = (a or b or c):Clone()

.... but this probably isn't what you want. It seems like you should be cloning chil[i] (or child as I recommend calling it) -- don't search for something with the same name, just use the object you have!

That would look like this:

event.OnServerEvent:connect(function(player, name)
    for _, child in pairs(workspace[name]:GetChildren()) do
        if child.Name == NormalParts[1] or child.Name == NormalParts[2] or child.Name == NormalParts[3] then
            local clone = child:Clone()
            clone.Parent = workspace
            clone.Transparency = 0
            print("Not brokenennn")
            break
        else

Your else doesn't make a lot of sense. If there is one part in there that isn't the right name, you want to fail? And you want to fail using a fancy transition, when there's obviously something very wrong?

0
Works in studio, not in play : < NotSoNorm 777 — 8y
0
How is "workspace" safer than "game.Workspace"? Discern 1007 — 8y
0
Someone could put another object in called "Workspace", or "Workspace" could be renamed. They're silly things, but there's basically no reason to have that level of indirection. That's why `workspace` is defined for you. BlueTaslem 18071 — 8y
Ad

Answer this question