Check if something exists

So I want to check if frame named “Copyright” exist, if does, it will kick the player instead.
Also, can I somehow require this script so they cannot edit it?

Code:

while true do
	if script.Parent.Main.Copyright ~= "" then
		for _,v in pairs (game.Players:GetChildren()) do
			v:Kick("Stealing is prohibited... ~ Pirate's Ranking Service")
		end
		script.Parent:Destroy()
	end
	wait(30)
end
14 Likes

i think you should use FindFirstChild, if it finds a child it will return it else it will return nill.

I think this code will work:

while true do
	if script.Parent.Main:FindFirstChild("Copyright") then
		for _,v in pairs (game.Players:GetChildren()) do
			v:Kick("Stealing is prohibited... ~ Pirate's Ranking Service")
		end
		script.Parent:Destroy()
	end
	wait(30)
end
12 Likes

Can it be a module, so I can require it noone can edit the script.

No one can change a script’s source (Only plugins). Clients can view the source with some strange exploits. And what do you mean? Please explain more and a bit of grammar.

4 Likes

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