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.

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
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.

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

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