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

GetChildren method help?

Asked by
JJ_B 250 Moderation Voter
9 years ago

I made a self destruct script that unanchors any parts in the game. Here is the script:

function onChatted(msg, recipient, speaker) 

-- convert to all lower case 

local source = string.lower(speaker.Name) 
msg = string.lower(msg) 

if (msg == "selfdestruct") and speaker.Name == "Wizardel" or "MoffatKiller" or "Player" then 
local p = game.Workspace:GetChildren()
    for i = 1,#p do     
        if p:IsA "Part" then
            p[i].Anchored = false

        end
        end 

end 
end

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered) 

THe output says "IsA is a nil value" Can someone help?

2 answers

Log in to vote
3
Answered by 9 years ago

Conditionals

When you make a conditional that deals with 'and', or, 'or', then you must constantly also note the variable which the conditional is being done on.

Also, using a barrage of 'or' to check for a player name may not be the best idea. This is where tables can come to an advantage. Additionally, in your case, I'd recommend using anonymous functions for a smooth near flow of code.

local allowedToDestroy = {"Wizardel","MoffatKiller","Player","DigitalVeer"}

function isAllowedToDestroy(plr)
local plrN = plr.Name;
for _,v in pairs(allowedToDestroy) do
if v:lower() == plrN:lower() then
return true;
end
end
return false;
end

function unanchorAll(loc)
 for _,v in pairs(loc:GetChildren()) do
  if v:IsA("BasePart") and v.Name ~= "Terrain" then
   v.Anchored = false
   else
  unanchorAll( v )
  end
 end
end

game.Players.PlayerAdded:connect(function(p)
 if isAllowedToDestroy(p) then
   p.Chatted:connect(function(msg)
      if msg:lower() == "selfdestruct" then
          unanchorAll(workspace)
       end
    end)
 end
end)


Though, to show you what was wrong with your conditional, you'd need something like this instead:

if msg == "selfdestruct" and speaker.Name == "Wizardel" or speaker.Name == "MoffatKiller" or speaker.Name == "Player" then

Also, you tried calling, 'isA' on an array however that method can only be done on actual instances.


Useful Links

These links should help you:

Conditionals: http://wiki.roblox.com/index.php?title=Conditional_statement

Anonymous Functions: http://wiki.roblox.com/index.php?title=Anonymous_function

Tables/Arrays: http://wiki.roblox.com/index.php?title=Table

1
Thanks a lot, this really helped. JJ_B 250 — 9y
Ad
Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I would just like to add to Digital's answer by speaking specifically of your problems.

When using or, your code should be in the format of;

if condition1 or condition2 or condition3 then

condition2 contains no references to condition1

This is your problem on line 8. You check the speaker's name fine the first time, but then after that you just write a couple of strings. Again, your first condition makes no effect on the second. They're 100% separate conditions, that will either be true or false on their own.

You therefore need to write the condition separately for each check;

--Spaced out to avoid annoying line wrapping
if msg == "selfdestruct" and 
    speaker.Name == "Wizardel" or 
    speaker.Name == "MoffatKiller" or 
    speaker.Name == "Player" then


Your second problem comes on line 11. p is a list (although that variable name doesn't make a lot of sense). A list is not a part.

What you want is what the list contains, and for that we need to write p[i]. Since you have a for loop changing the value of i, that means p[i] will give you the ith value in the list (since i is a number).

for i = 1,#p do    
    if p[i]:IsA("Part") then--Adding parentheses is the preferred method for calling a function
        p[i].Anchored = false
    end
end

1
Yep! Just as Perci has stated, each conditional is independent of the others. +1 Perci DigitalVeer 1473 — 9y

Answer this question