Run Loop When None Player Left Touching a Part

I have while loop and i just want to run it when none player left on a part. But the while loop in ServerScriptService.

How can i make something like

--ServerScriptService
While TouchingPlayersToPart = 0 do
--things
end
1 Like

Hi there. I suggest checking out WorldRoot | Documentation - Roblox Creator Hub

1 Like

I am so new to understand thoose for scripting T-T

maybe do something like

--ServerScriptService
function doThis()

end

While true do
   if TouchingPlayersToPart = 0 then 
      doThis()
   end
end

1 Like

But how to dedect if TouchingParts = 0…

wait i find better code:

local part: BasePart = script.Parent
local currently_touching = {}

part.Touched:Connect(function(BasePart)
	if game:GetService('Players'):GetPlayerFromCharacter(BasePart.Parent) then
		table.insert(currently_touching, game:GetService('Players'):GetPlayerFromCharacter(BasePart.Parent))
		
		while currently_touching[game:GetService('Players'):GetPlayerFromCharacter(BasePart.Parent)] do task.wait()
			print("Player is still touching.")
		end
		
	end
end)

part.TouchEnded:Connect(function(BasePart)
	if game:GetService('Players'):GetPlayerFromCharacter(BasePart.Parent) then
		table.remove(currently_touching, table.find(currently_touching, game:GetService('Players'):GetPlayerFromCharacter(BasePart.Parent)))
	end
end)

No wait i think is trash T-T
Give me a second i will do it myself

1 Like

So the WorldRoot is I believe the Workspace. So game.Workspace
The syntax would look something like game.Workspace:GetPartsInPart(part, overlapParams)

Since this returns an array of all the parts inside of the part you’re testing on, you can check the length of the array if it is 0.
while #game.Workspace:GetPartsInPart(part, overlapParams) <= 0 do

@iQeeDEVS edited

1 Like

Try this code:

local Players = game:GetService(“Players”)
local ServerScriptService = game:GetService(“ServerScriptService”)

local part = game.Workspace.Part – Replace “Part” with the name of your part

local function checkPlayersInPart()
while #part:GetTouchingParts() == 0 do
– Your code inside the while loop
print(“No players in the part”)
wait(1)
end
end

Players.PlayerAdded:Connect(function(player)
player.Changed:Connect(function(property)
if property == “Parent” and player.Parent == nil then
checkPlayersInPart()
end
end)
end)

Players.PlayerRemoving:Connect(function(player)
if #part:GetTouchingParts() == 0 then
checkPlayersInPart()
end
end)

1 Like
local part = game.Workspace.YourPart -- Replace 'YourPart' with the actual name of your part

while true do
    local touchingParts = part:GetTouchingParts()

    local touchingPlayers = {}
    for _, touch in pairs(touchingParts) do
        if touch:IsA("Player") then
            table.insert(touchingPlayers, touch)
        end
    end

    if #touchingPlayers == 0 then
        -- Your code here (things you want to do when no players are touching the part)
    else
        -- Code to handle when there are players touching the part if needed
    end

    wait(1) -- Adjust the wait time based on your needs to avoid excessive loop execution
end

2 Likes
2 Likes

Thanks for all replies, i just tried this one first. But it keeps looping even someone still on the part.


local part = workspace.Part -- Replace 'Part' with the actual name or reference to your part

function isPlayerTouching()
    local touchingParts = part:GetTouchingParts()

    for _, object in pairs(touchingParts) do
        if object:IsA("Player") then
            return true  -- Player is touching the part, exit function with true
        end
    end

    return false -- No player found among the touching parts
end

while not isPlayerTouching() do
    -- Your code here (will execute as long as no players are touching the part)
    wait(1) -- Adjust the wait time as needed to control the loop frequency
end

Thank you but it still keep looping, theres not even one error. But it won’t stop while touching part.

try this

local part = workspace.Part -- Replace 'Part' with the actual name or reference to your part

local function onPartTouched(other)
    if other:IsA("Player") then
        -- Player entered the part
        print("Player entered the part")
        -- Add any additional logic you want to execute when a player enters the part
    end
end

local function onPartTouchEnded(other)
    if other:IsA("Player") then
        -- Player left the part
        print("Player left the part")
        -- Add any additional logic you want to execute when a player leaves the part
    end
end

part.Touched:Connect(onPartTouched)
part.TouchEnded:Connect(onPartTouchEnded)

-- Wait for players to finish touching the part
while true do
    wait(1)  -- Adjust the wait time as needed to control the loop frequency
end

1 Like

Sir, is that working for you, did you try it? Because it won’t print anything.

i think i did it:

local ps = game:GetService("Players")
local rs = game:GetService("RunService")
local part = script.Parent

local maxTime = 2 -- in seconds
local leftTime = 0 -- or maxTime

local timeBetweenEvents = 5
local timeLeftForEvent = timeBetweenEvents

function partLeftAlone()
	part.Color = Color3.fromRGB(255, 0, 115)
end

function partStart()
	part.Color = Color3.fromRGB(0, 255, 162)
end

function findPlayer(part)
	return ps:GetPlayerFromCharacter(part.Parent)
end

function partTouched(hit)
	local plr = findPlayer(hit)
	if plr then 
		if leftTime <= 0 then
			partStart()
		end
		leftTime = maxTime
	end
end

function checkPlayerTouching()
	local parts = part:GetTouchingParts()
	for index, partTouching in pairs(parts) do
		local plr = findPlayer(partTouching)
		if plr then
			leftTime = maxTime
			break
		end
	end
end

function executeEvent()
	part.Anchored = true
	part.Position += Vector3.new(0,0.6,0)
end

function updateTime(delta)
	if leftTime <= 0 then
		return
	end

	leftTime -= delta 
	
	if leftTime <= 0 then
		partLeftAlone()
		timeLeftForEvent = timeBetweenEvents
	else
		if timeLeftForEvent > 0 then
			timeLeftForEvent -= delta
			checkPlayerTouching()
		else
			executeEvent()
			timeLeftForEvent = timeBetweenEvents
		end
	end
end

part.Touched:Connect(partTouched)
rs.Heartbeat:Connect(updateTime)


when someone touches it becomes green, if its left alone for 1 second, becomes red, if its is hold for 5 seconds it goes up a bit, if 5 seconds pass again holding the part goes up a bit again, if its left alone the event time will reset, fell free to change the time between events to 1 seconds, or the time the part stays green after no touching players to 0

Adapt the code to your game obviously

1 Like

This is so cool and works but there a problem, if player didn’t touch part it wouldn’t know the player left. But i want to work the function always when there no one on part.
In your script function works if the player touched and left.

This is a newer version of it
Introducing Shapecasts - Updates / Announcements - Developer Forum | Roblox

1 Like

They are completely different features.

1 Like

Try this I modified Luca1006’s script
updateTime function checks if there are no players touching the part, and if so, it calls the partLeftAlone function. This way, the function will be triggered even if the player never touched the part.

local ps = game:GetService("Players")
local rs = game:GetService("RunService")
local part = script.Parent

local maxTime = 2 -- in seconds
local leftTime = 0 -- or maxTime

local timeBetweenEvents = 5
local timeLeftForEvent = timeBetweenEvents

function partLeftAlone()
    part.Color = Color3.fromRGB(255, 0, 115)
end

function partStart()
    part.Color = Color3.fromRGB(0, 255, 162)
end

function findPlayer(part)
    return ps:GetPlayerFromCharacter(part.Parent)
end

function partTouched(hit)
    local plr = findPlayer(hit)
    if plr then 
        if leftTime <= 0 then
            partStart()
        end
        leftTime = maxTime
    end
end

function checkPlayerTouching()
    local parts = part:GetTouchingParts()
    for index, partTouching in pairs(parts) do
        local plr = findPlayer(partTouching)
        if plr then
            leftTime = maxTime
            break
        end
    end
end

function executeEvent()
    part.Anchored = true
    part.Position += Vector3.new(0,0.6,0)
end

function updateTime(delta)
    local parts = part:GetTouchingParts()
    if #parts == 0 then
        partLeftAlone()
    end

    if leftTime <= 0 then
        return
    end

    leftTime -= delta 
    
    if leftTime <= 0 then
        partLeftAlone()
        timeLeftForEvent = timeBetweenEvents
    else
        if timeLeftForEvent > 0 then
            timeLeftForEvent -= delta
            checkPlayerTouching()
        else
            executeEvent()
            timeLeftForEvent = timeBetweenEvents
        end
    end
end

part.Touched:Connect(partTouched)
rs.Heartbeat:Connect(updateTime)