Needing help with region3s playing sounds

I’m trying to make region3 zones that play a start sound and a looping sound for a grinding a skateboard.

The region3s are made with part position and stuff that are in a folder in workspace, ‘Rails’ and ‘Ledges’ and it is working perfectly with one part in the folders but as soon as theres more then that it starts looping sounds and it just generally breaks.

I’ve tried with help from a friend to think of ways to fix, I thought to make a table for the parts/region3s but im not too sure how to do that.

This is what happens when you go on a rail when theres 2 parts in the folder, doesnt matter what part you jump on the same thing happens. You can see it in the video.

while wait(.1) do
    for i, v in pairs(rails:GetChildren()) do
        OnRail = false
        local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
        local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region,game.Players.LocalPlayer.Character:GetDescendants())

        for _, part in pairs(parts) do
            if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
                OnRail = true
                break
            else
                OnRail = false
                break
            end
        end
        if OnRail == true then
            if FakeRailLoop.IsPlaying == false then
                Remotes.Sound:FireServer("startrail")
                FakeRailLoop:Play()
                FakeRailStart:Play()
                break
            end
        else
            FakeRailLoop:Stop()
            Remotes.Sound:FireServer("stoprail")
        end
        for i, v in pairs(ledges:GetChildren()) do
            OnLedge = false
            local region2 = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
            local parts2 = game.Workspace:FindPartsInRegion3WithWhiteList(region2, game.Players.LocalPlayer.Character:GetDescendants())

            for _, part in pairs(parts2) do
                if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
                    OnLedge = true
                    break
                else
                    OnLedge = false
                    break
                end
            end
            if OnLedge == true then
                if FakeLedgeLoop.IsPlaying == false then
                    Remotes.Sound:FireServer("startledge")
                    FakeLedgeLoop:Play()
                    FakeLedgeStart:Play()
                    break
                end
            else
                FakeLedgeLoop:Stop()
                Remotes.Sound:FireServer("stopledge")
            end
        end
    end
end

I do not understand why you are using Region3 rather than BasePart.Touched and BasePart.TouchEnded. I guess that you can just start playing the sound when your skateboard is touching a rail and end it when the touch ends.

Im going to be honest i didnt know about TouchEnded, ill give it a test, thanks!

I cant get it to work and I probably would rather stick with region3.

Can you try this? You will have to define skateBoardPart first.

local startAndLoopSounds = {
	ledge = {
		start = FakeLedgeStart,
		loop = FakeLedgeLoop,
	}
	rail = {
		start = FakeRailStart,
		loop = FakeRailLoop,
	}
}

local isOnRail = false
local isOnLedge = false

local function playSound(soundName)
	local sounds = startAndLoopSounds[soundName]
	sounds.start:Play()
	sounds.loop:Play()
	Remotes.Sound:FireServer("start" .. soundName)
end

local function stopSound(soundName)
	local sounds = startAndLoopSounds[soundName]
	sounds.loop:Stop()
	Remotes.Sound:FireServer("stop" .. soundName)
end

skateBoardPart.Touched:Connect(function(touchedPart)
	if touchedPart:IsDescendantOf(ledges) and (not isOnLedge) then
		-- Touched a ledge
		isOnLedge = true
		playSound("ledge")
	elseif touchedPart:IsDescendantOf(rails) and (not isOnRail) then
		-- Touched a rail
		isOnRail = true
		playSound("rail")
	end
end)

skateBoardPart.TouchEnded:Connect(function(touchedPart)
	if touchedPart:IsDescendantOf(ledges) and isOnLedge then
		isOnLedge = false
		stopSound("ledge")
	elseif touchedPart:IsDescendantOf(rails) isOnRail then
		isOnRail = false
		stopSound("rail")
	end
end)

touchended is really buggy, i put a print on the touchended part and when im on the rail it just loops.

i think i should make like multiple instances of the region or something like that if that makes sense.

FindPartsInRegion3 is not accurate and will expand the region to voxels (4x4x4) and check if the part is roughly in that area, have you tried moving them further apart? Or you can just have a hitbox part and use GetTouchingParts

the region3 is the hitbox i thought? moving them further apart still has the same result.

what do you mean about ‘buggy’, just that it is doing it in a loop? can you add a debounce in it so it does not loop ?

its just buggy idk

Have tried looking at this module it uses region3 and has player entered and player exited.;

1 Like

would that work inside of the localscript?

Yes it would since it’s just a region3.

yeah thanks with the zone thing i made it work.