Repeat wait() until Part == nil not working

I’m trying to check if a part is equal to nil, but it doesn’t seem to work. My end goal here is to check if a part has been touched, then when it is touched it destroys itself. Then the main script will repeat wait() until part == nil, then it will perform some action.

This is the main script:

local showDialogue = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("ShowDialogue")
local setObjective = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("SetObjective")

local exitHospitalTrigger = game.Workspace:WaitForChild("Map"):WaitForChild("Triggers"):WaitForChild("DialogueTriggers"):WaitForChild("ExitHospitalTrigger")

local function startGame()
	showDialogue:FireAllClients("")
	setObjective:FireAllClients("")
	
	showDialogue:FireAllClients("Huh? Where am I?")
	wait(4)
	showDialogue:FireAllClients("It kinda looks like an abandoned hospital...")
	wait(4)
	showDialogue:FireAllClients("Anyways, I need to get out of this place.")
	wait(4)
	setObjective:FireAllClients("Get out of the abandoned hospital.")
	
	repeat wait() until exitHospitalTrigger == nil
	
	showDialogue:FireAllClients("")
	setObjective:FireAllClients("")
	
	showDialogue:FireAllClients("I finally got out of that mysterious hospital...")
	wait(4)
	showDialogue:FireAllClients("But, I think I'm lost.")
	wait(4)
	showDialogue:FireAllClients("Oh, I'm screwed...")
	wait(4)
	setObjective:FireAllClients("Find your way back home.")
end

wait(5)
startGame()

And the part’s script:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		script.Parent:Destroy()
	end
end)

This is the explorer if you need it:

And some gameplay:

As you can see, for some reason, it gets stuck on “Get out of the abandoned hospital” and it doesn’t progress to the next objective even though the part has been destroyed.

Help would be appreciated.

Instead of waiting for the part to destroy itself, you can implement a safer solution by firing a bindable event for the second script to receive, so I am assuming your scripts should look like the following:

Part Destroy:

local event = script.DestroyEvent
local part = script.Parent

part.Touched:Connect(function()
event:Fire()
end)

This allows you to use the .Event event for the bindable event you’ve set up at any script you wish, and the code within will start executing.

Edit: To block the event from running multiple times due to how the .Touched event works, I’d suggest adding a debounce:

local event = script.DestroyEvent
local part = script.Parent
local debounce = false

part.Touched:Connect(function()
if debounce == false then
event:Fire()
debounce = true
end
end)
2 Likes

I couldn’t have done it without you! Thank you so much!

1 Like