Check if something exists

So, basically I want to require that script, but the script wouldn’t be in-game (studio). It would be required with a script to load a module one.

to require a module only in-game (Not studio) You need the function IsStudio to check if the script is running in Roblox Studio. To require a module only in Roblox studio you need this script:

if game:GetService("RunService"):IsStudio then
        -- Do things
end 

and if you want a script run only in the game you need this script:

if not game:GetService("RunService"):IsStudio then
        -- Do things
end 

Thanks, it is now working. (30 characters)

1 Like

Nevermind, it actually kicks a player if that exists, I’m continuing to look.

The script you gave us kick all the players if a object exists,what do you really want?

Kick player if that object does not exist.

You should use

if not script.Parent.Main:FindFirstChild("Copyright") then

end
22 Likes
local part = game.workspace.Part
if part == nil then do 
    -- do what you wan't
end)

I think this could work too, but I might be wrong.

1 Like

this Should not work becouse if Part is nil it will error, the correct script whould be

local part = game.workspace:FindFirstChild("Part")
if part == nil then do 
    -- do what you wan't
end)

8 Likes

I’m silly. Thank you again. (30 chars)

1 Like

If I’m correct you’d want to kick the player if they deleted the “Copyright” frame locally. This means you’d need to check the player every 30 seconds or so (depending on the interval you want) to see if they had the “Copyright” frame in their PlayerGUI. This script will only work on the server and will not work on the client (localscripts)

while true do
	for i,player in pairs(game.Players:GetPlayers()) do
		if player.PlayerGui.NAMEOFGUIHERE.Main:FindFirstChild("Copyright") ~= true then
			player:Kick("whatever kick message you'd want here")
		end
	end
	wait(30)
end

For assistance on learning to use ModuleScripts the best place to look is the Roblox Wiki or to open another support thread with this specific issue, please try and use community resources first though.

1 Like

or you could do

local main = script.Parent.Main
    if not main --if main does not exist  
      then  return
     print("Main does not exist")
    end

No, those will both error if it doesn’t exist. Turtle has already said this.

4 Likes

even if it returns end even? Wow I didn’t know that, thanks

Yeah, if an instance doesn’t exist you have to use FindFirstChild. Trying to access it by indexing directly doesn’t work. This is why FindFirstChild is a thing.

3 Likes

@TheTurtleMaster_2 has the correct solution. (Please do note to use the capital W for Workspace or use the workspace global)

I do want to explain why things like this doesn’t work:

local part = workspace.Part
if part == nil then do

end

Your trying to define a Part in workspace as “part”. Instead of “part” being defined as nil, it gives you an error at that line when your defining that variable since it doesn’t exist.

FindFirstChild method is the best way to check if something exists. You should use FindFirstChild only when your not sure if something will exist at that time and you want to check. Also note that FindFirstChild is about 20% slower than using “.” to index something.

1 Like

maybe im a bit too late because as TheTurtleMaster_2 said using “if not” is an option but im pretty sure using if script.Parent.Main:FindFirstChild(“Copyright”) == nil then… will also work

When I was a beginner, I had this mistake too.

You could try

local SPM = script.Parent.Main
if not SPM
then return
print(“does not exist”)
end

1 Like

Hey! To make this better and to optimize it to reduce memory/potential lag, I would instead do

script.Parent.ChildRemoved:Connect(function(instance)
    if not script.Parent:FindFirstChild("Copyright") then
        -- what to do if copyright doesn't exist
    end
end)

hope this helped :smiley:

5 Likes

that’s a very good method nice job

2 Likes