How come my isA("Script") function doesn't work?

Ok, I won’t explain exactly what this is for, but basically the script will loop through an object’s descendants and see if one of them is a script, and if it is, enable it. However, it doesn’t work. Here’s the object in question.
Screenshot 2024-07-20 at 11.20.25 AM
Here’s the code.

for _, scriptObject in pairs(item:GetDescendants()) do
							if scriptObject:isA("Script") then
								scriptObject.Enabled = true
							end
						end

Here’s the result:

Can someone please help me?

6 Likes

I may be wrong but could it be a capitalisation error? Make sure the I is capitalised in IsA

7 Likes

Was going to say that but could not because terrible wifi

2 Likes

Few things you could try

  • Have you tried just printing the object names?
  • Have you tried just finding the first child with the specific script name? (no need for a loop that way ;0)
  • Try using pcall’s/xpcall’s (you can research them using the docs)
3 Likes

Are you running the code on the client? If yes then running script.Enabled = true will probably not activate a normal server script.

3 Likes

No this is a server script not a client sided script

4 Likes

I hope me telling you guys what the game is will help you produce better responses.
This is a tycoon game and so whenever you step on a button it will link “item” to the corresponding item.
It will then loop through the item’s descendants to see if there’s a script.

@thatguybarny1324,
If I use FindFirstChildOfClass, it could return an error (I think)
I havent tried the other tips yet I’ll try them soon.

@EIitium,
I don’t think it’s that, when I did IsA it still didnt work so I switched it to isA which is the exact same thing I believe

2 Likes

I’d just use FindFirstChildWhichIsA(“Script”) on the model instead of a loop if there’s only one script

(Also at the base level, whatever you’re doing could be done a lot easier once you know more about scripting)

1 Like

only if it doesn’t exist it will return nil. you could do this to check:

local script = nil -- find first child code here
if script == nil then
-- error print
end
2 Likes

Did you read this message? I think not.

3 Likes

Thanks I’ll be sure to try that and see if it works

2 Likes

If the template is in replicated storage/server storage you can also just leave the script enabled (since it won’t run until it’s out)

2 Likes

If you have multiple scripts in the descendants of the item then the script which you provided will only get one of the scripts then Enable it and ignore the rest, to fix this, create a table and store all of the script instances in there, and use another for i, v in ipairs loop to manipulate the property Enabled for each individual script in the table. You can clear the table later if you want.

local all_scripts = {}

for _, scriptObject in ipairs(item:GetDescendants()) do
							if scriptObject:isA("Script") then
								table.insert(all_scripts,scriptObject)
							end
						end

for i , v in ipairs(all_scripts) do 

v.Enabled = true

end

Tell me if it works.

2 Likes

I think that you have to use ipairs instead of pairs as I got a silent error while iterating through players with pairs, but when I changed it to ipairs, it worked. Test it and tell me if it works.

Also, @yousefoyoy, that doesn’t change nothing. GetDescendants returns a table with Instances, and you’re just inserting that table content to another table.

2 Likes

Actually you don’t really need pairs or ipairs in Roblox because it uses Luau. It’s actually slightly faster not to sometimes (in writing and in speed) so that shouldn’t really matter (and in this case, it doesn’t matter what order the instances get checked)

2 Likes

But ipairs iterates through tables in a different way than pairs.

2 Likes

Yes, I know that, but I was just pointing out that neither versions mattered in this case

2 Likes

I think that pairs can’t iterate through arrays, and GetDescendants returns arrays, so?

2 Likes

I disagree, I’ve used this method many times and it always works. For example: if I have an if statement inside a for i , v in ipairs(whatever) it will only loop through one of the instances which is in the whatever and the script just breaks.

2 Likes

pairs can iterate both dictionaries and arrays but doesn’t iterate in order. ipairs, though faster, can only iterate through arrays and is in order, skipping nil values. However, generalized iteration is better now.

OP, run your game and check if the script is being enabled in the explorer view. If so, it might just be the script itself somehow not working.

2 Likes