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

How to detect if the player is not in the region anymore?

I have made a region, that when you enter it, it plays a piece of music, and when the player leaves the music stops.

It seems to be playing OK

I have rigorously scrutinized the script, nothing seems wrong but it does not function properly.

It still plays when i leave the region?

It would be an honor if i could get some help.

local RegionPart = game.Workspace.Region
local Debounce = false
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local Region = Region3.new(pos1, pos2)
local Lantern = game.Workspace.Lantern

while true do
	wait()
	local PartsInRegion = workspace:FindPartsInRegion3(Region, Lantern, 1000)
	for i, part in pairs(PartsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil and not Debounce then
			Debounce = true
			print("Player is currently in the deep dark cave")
			game.Workspace.CaveMusic:Play()
			
			while true do
				wait()
		if workspace:IsRegion3Empty(Region, Lantern, 1000) then
			print("It's empty")
			game.Workspace.CaveMusic:Pause()
			end
			
			
		end
		end
		end
		end

wait(1)
Debounce = false

1 comment

0

If you have different blocks separating the region from the non-region, you can just use a touched function to disable the music?

1 answer

✓ Accepted answer

Sorry for the late response. I might be wrong, I'm on mobile so I can't test. But I think the problem with your script is the while true do statement. The problem with using a while true do inside another while true do statement is that if you never break that second while true do statement, all the code below that second while true will never run. Here's what I would use instead:

local RegionPart = game.Workspace.Region
local Debounce = false
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local Region = Region3.new(pos1, pos2)
local Lantern = game.Workspace.Lantern

while true do
    wait(1)
    local PartsInRegion = workspace:FindPartsInRegion3(Region, Lantern, 1000)
    for i, part in pairs(PartsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil and not Debounce then
			Debounce = true
			print(part.Parent.." is currently in the deep dark cave")
			game.Workspace.CaveMusic:Play()
		else
			print("It's empty")
			game.Workspace.CaveMusic:Pause()
		end
	end
	Debounce = false
end

Once again I'm on mobile so there is no way for me to test if this works, if you encounter any problems let me know.

1 comment

0

thanks this will make my scripting career!!! :D

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