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?
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.
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
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 i
th 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