Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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.

01while true do
02    if workspace["Core"] then
03        print("Core Exists")
04    else
05        print("ERROR: CORE MISSING")
06        script.Parent:Play()
07        local H = Instance.new("Hint",game.Workspace)
08        H.Text = "ERROR: Core Model is Missing or Destroyed. The server will be shut down in about 1 minute."
09        wait(60)
10        for i,v in pairs(game.Players:GetPlayers()) do
11            v:Kick("Server was shut down due to the Core being missing or destroyed.")
12        end
13        break
14    end
15    wait(1)
16end
0
Note: I've also tried "Instance:FindFirstChild", "workspace.Core", and "if workspace.Core == nil then" AzrefriskDreemurr 8 — 5y
0
if workspace:WaitForChild("Core", 3) then DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago

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

01function onChildRemoved(removedInstance)
02    if removedInstance.Name == "Core" then
03        for i,v in pairs(Players:GetPlayers()) do
04            v:Kick("Server shutdown!")
05        end
06    else
07        print("Phew, core is still here :D")
08    end
09end
10 
11workspace.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

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. Mr_Unlucky 1085 — 5y
0
Thank you! I was unaware of the ability to fire code upon part removal. AzrefriskDreemurr 8 — 5y
Ad

Answer this question