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

How do I check if a part is still in the Workspace?

So I'm making a game with a minigame where players delete parts and there's a certain part they have to delete. If they delete the part, the game should end, but I have no way of checking if that part is still in the workspace or not. I tried to use an if statement I see on youtube videos, but that doesn't work. Here's an example of how I'm trying to see if the part is there or not:

if game.Workspace.GoldPart then
	print("The gold part has not been deleted!")
end

The problem with this is that if the part is not there, an error will occur saying something like "GoldPart is not a valid member of Workspace" or something like that.

3 answers

✓ Accepted answer

Alright so, you can use both of the above things, but i think this would be more efficient:

if game.Workspace;FindFirstChild("GoldPart") then
	print("Game hasn't ended yet.")
end

This is a way i would use though:

local Nilly = false
repeat
	wait()
	if not game.Workspace;FindFirstChild("GoldPart") then
		Nilly = true
	end
until Nilly
print("Game has ended!")

And yes i'm calling my local variable Nilly, don't judge.

- CrazyScript3r

1 comment

0

I tried yours and it worked just like the other two, but I think I'm going to stick with yours just because it uses "repeat" and "until" which is more secure, in my opinion. I also liked the fact that you use Nilly for your variables :p

First of all you need a loop to check repeatedly if that part exists, which is going to look something like this for example:

while wait(1) do -- do this every 1 second
    if not workspace:FindFirstChild("GoldPart") then
        print("Game has ended") 	
    end
end

That's it, if you have any questions please let me know!

local goldpart = game.Workspace:FindFirstChild("GoldPart")

if goldpart then
	print("The gold part has not been deleted!")
end

Try this :D

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