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

Checking for an object like this isn't working, Whats wrong with this?

I'm basically implementing a script where it plays an audio object and shuts down the server after a minute if the main object in the game goes missing.

while true do
	if workspace["Core"] then
		print("Core Exists")
	else
		print("ERROR: CORE MISSING")
		script.Parent:Play()
		local H = Instance.new("Hint",game.Workspace)
		H.Text = "ERROR: Core Model is Missing or Destroyed. The server will be shut down in about 1 minute."
		wait(60)
		for i,v in pairs(game.Players:GetPlayers()) do
    		v:Kick("Server was shut down due to the Core being missing or destroyed.")
		end
		break
	end
	wait(1)
end

2 comments

0

Note: I've also tried "Instance:FindFirstChild", "workspace.Core", and "if workspace.Core == nil then"

0

if workspace:WaitForChild("Core", 3) then

1 answer

✓ Accepted answer

Well I think the problem here is that you're using a loop when you should be using Events instead.

function onChildRemoved(removedInstance)
	if removedInstance.Name == "Core" then
		for i,v in pairs(Players:GetPlayers()) do 
			v:Kick("Server shutdown!")
		end
	else
		print("Phew, core is still here :D")
	end
end

workspace.ChildRemoved:Connect(onChildRemoved)

In the code above we have a function that fires every time a child is removed from the workspace. The function checks if the instance that was removed was the Core, and if that's the case we kick everyone. If the core is still there then we will do nothing.

Click here for more information on child removed

2 comments

0

Keep in mind this is an example, I didn't add the audio or the message. You're just going to have to add it yourself.

0

Thank you! I was unaware of the ability to fire code upon part removal.

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