If statement not working properly

local function ApplyTemplate(Script: Instance)
	if game:GetService("RunService"):IsRunning() then
		return
	end
	
	task.wait(1)
	print(Script.Name)
	if Script.Name ~= "Script" and Script.Name ~= "ModuleScript" and Script.Name ~= "LocalScript" then
		return 
	end

    --... more code, unrelated.
end

game.DescendantAdded:Connect(ApplyTemplate)

Hello everyone!
On this line here:

if Script.Name ~= "Script" and Script.Name ~= "ModuleScript" and Script.Name ~= "LocalScript" then
	return 
end

The script doesn’t return it. This is in a LocalScript [For a plugin]. The script print’s the correct name of the script, e.g “DataController”, but it never returns. Does anyone have any idea why?

I am trying to make a plugin for myself that does something to a script, but I cannot get passed this.

Any support will be appreciated, thanks. Been working for 30 minutes trying to fix this issue :sob:

2 Likes

Two options:

  1. Script is a defined variable, in this case let me know what “Script” is
    or the second option
  2. If you refer to the script you entered the code in, it would be “script” instead of “Script”

Besides that, what is the code used for?

“Script” is passed through via game.DescendantAdded, and I’m not trying to refer to the current script that the code is in, I’m trying to refer to the new instance added.

Also the code is part of a larger project which I don’t want to say :wink:

You could try printing Script.Name before running the if statement, but i assume you already have tried that.

I want it to not modify the script if it already contains code // has a different name

I have, and it prints DataController

Your issue is that your looking at the name and not the Class name so would work:

if Script.ClassName ~= "Script" and Script.ClassName ~= "ModuleScript" and Script.ClassName ~= "LocalScript" then
	return 
end

But I would use the IsA function which is:

if not (Script:IsA("Script")  and Script:IsA("ModuleScript") and Script:IsA("LocalScript") ) then
	return 
end

i reread your post and realised what i said didnt make sense lmfao

1 Like
local function ApplyTemplate(Object: Instance)
	if game:GetService("RunService"):IsRunning() then
		return
	end
	
	task.wait(1)
	print(Object.Name)
	if Object.Name ~= "Script" and Object.Name ~= "ModuleScript" and Object.Name ~= "LocalScript" then
		return 
	end
	
	if Object:IsA("Script") then
		if Object.Source == "" then
			Object.Source = [[
			]]
		end
		
	elseif Object:IsA("ModuleScript") then
		Object.Source = [[
]]
	
	elseif Object:IsA("LocalScript") then
		Object.Source = [[
]]
	end
end

Maybe this helps provide context on what I want to do? I don’t want it to return if it’s not a Script, but I want it to return if the Script already has a name, because that likely means it already has code inside, and it shouldn’t replace said code.

I am assuming you wanna detect the class, otherwise I would try this approach:

local Names = {
	["ModuleScript"] = 1,
	["LocalScript"] = 1,
	["Script"] = 1,
}

if not Names[script.Name] then 
	return
end

It checks if it is the dictionary, if not then return

A question from my side: Is .ClassName doing the same as typeof()?

.ClassName is the class of an object like a LocalScript would have the ClassName of LocalScript, or a part would have one of Part. ETC

I will try this out, give me a moment.

Wouldn’t you have to do

if not Script:IsA("Script") and not Script:IsA("ModuleScript") and not Script:IsA("LocalScript") then
    return
end

I have found the cause, previously I never added that if statement in, so I didn’t realise it, but every script just contains that code now, so that’s my fault.

The if statement does return, but the code is just bugged, whoops.

Type of returns the data type (instance, boolean, number, string, table, etc…).
typeof(Instance.new("Part")) → "Instance"
Instance.new("Part").ClassName → "Part"

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.