Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my if statement call other things a script? [SOLVED]

In my code, there's a function. In that, there's an if statement that sees if the object is a script. When it sees something other than a script, it calls it a script, which continues with the script and causes an error. Can anyone fix this? Thanks in advance!
I used "IsA" already and "ClassName" string. They both didn't work. The code below is all I'll provide

local function Scan(a)
if a.ClassName == "Script" or "LocalScript" then -- problem

EDIT: I just figured it out myself. If you're experiencing this problem, here is the code:

local function Random(a)
	local success,data = pcall(function()
		if a.Source then
			Scan(a)
		end
	end)
end
local function Scan(a)
--Here you can proceed the code. Of course you can use the pcall here

6 comments

0

Can you provide the code you're explaining? It will help us understand the problem better.

0

I don't think you can edit the class of an object

0

That's now what I'm saying. The code will make you understand it better now.

0

Used a for i, v in pairs loop

0

Use*

0

And you can't just say "LocalScript", you have to say a.ClassName == "LocalScript" then

1 answer

As your code stands, it compares the ClassName of a to "Script". If this was false it will look at the or logical operator and see that "LocalScript" (Which is a string) is true. This is because in Lua, values that are not nil or false are evaluated as being truthy.

Your code basically says:

if a.ClassName == "Script" or true then

end

The if-statement always passes since the or sees that the next thing is true. We need to compare the string to ClassName again to fix this.

if a.ClassName == "Script" or a.ClassName == "LocalScript" then

end

Let me know if I explained that right for you to get. Maybe someone else will correct me if I did miss anything.

2 comments

0

I fixed the code, and it didn't work. Same with IsA. However, thank you for your answer.

0

This should fix the if-statements. If your code you provided is what you have then check to see if you are missing the enclosing end required for the "Scan()" function.

This discussion is preserved as it was. New answers, comments, and votes are closed.