Cutscene triggers if all players are in a zone

How could I check a userId though in a server script? I tried another way by adding a boolvalue in the player so that if IsInElevator = true then just skip the parts. But I am having trouble detecting if all players already touched it. This is my script right now

local model = script.Parent
local primary = model.PrimaryPart
local tweenservice = game:GetService("TweenService")
local debounce = false
local player = game:GetService("Players").PlayerAdded:Wait()

function ending()
	workspace["PM (PlaceholderMusic)"]["Elevator Jammed"]:Play()
	task.wait(1)
	local tween = tweenservice:Create(primary,TweenInfo.new(25.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {CFrame = primary.CFrame + Vector3.new(0,-175,0)})
	tween:Play()
	tween.Completed:Wait()
	local ahhhhtween = tweenservice:Create(primary,TweenInfo.new(15, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {CFrame = primary.CFrame + Vector3.new(0,-2000,0)})
	ahhhhtween:Play()
end

primary.Touched:Connect(function(hit)
	print(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local players = game.Players:GetPlayers()

		for _, player in pairs(players) do
			local count = 0
			player.Character.HumanoidRootPart.Touched:Connect(function(e)
				if e == primary then
					count += 1
					player.Character.IsInElevator.Value = true
					if player.Character.IsInElevator.Value == true then 
						local weld = Instance.new("WeldConstraint")
						hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
						weld.Parent = model
						weld.Part0 = hit.Parent.HumanoidRootPart
						weld.Part1 = primary
					end
				end
				-- Check if all players are touching the primary object
				if count == #players then
					if player.Character.IsInElevator.Value == true then
						if not debounce then
							debounce = true
							local anim = Instance.new("Animation")
							anim.AnimationId = "rbxassetid://103845461454072"
							local rahhhh = hit.Parent.Humanoid:LoadAnimation(anim)
							rahhhh:Play()
							hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
							task.wait(0.3)
							print("players: "..#players)
							print("count: "..count)
							ending()
							task.wait(13.8)
							player.PlayerGui.DeadGui.Enabled = true
						end
					end
				end
			end)
		end
	end
end)

wait(5)

local primary = workspace:WaitForChild("e")

local players = game.Players:GetPlayers()
local touchedPlayers = {} -- Table to track players
local count = 0 

for _, player in pairs(players) do
    player.Character.HumanoidRootPart.Touched:Connect(function(e)
        if e == primary then
          
            if not touchedPlayers[player.UserId] then
                touchedPlayers[player.UserId] = true 
                count += 1 
            end
        end
        if count == #players then
            print("Everyone is touching the part")
        end
    end)
end

You might have to clear the table at some point after each check. i don’t know I didn’t test with 2 plrs

It works at last! But I want it to work with all players because it can only do it to one player. So how can I do that? I tried doing remote events by doing event:FireClient(player, model) but it didn’t work. This was the script I had that receives the event. It’s a local script inside the starter character.

local event = game.ReplicatedStorage.Events:WaitForChild("ElevatorEnd")

event.OnClientEvent:Connect(function(model)
	print("im here!")
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://103845461454072"
	local rahhhh = script.Parent.Humanoid:LoadAnimation(anim)
	rahhhh:Play()
	script.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
	task.wait(13.8)
	game.Players.LocalPlayer.PlayerGui.DeadGui.Enabled = true
end)

this is the elevator main script

local model = script.Parent
local primary = model.PrimaryPart
local tweenservice = game:GetService("TweenService")
local debounce = false
local player = game:GetService("Players").PlayerAdded:Wait()
local event = game.ReplicatedStorage.Events:WaitForChild("ElevatorEnd")

function ending()
	workspace["PM (PlaceholderMusic)"]["Elevator Jammed"]:Play()
	task.wait(1)
	local tween = tweenservice:Create(primary,TweenInfo.new(25.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {CFrame = primary.CFrame + Vector3.new(0,-175,0)})
	tween:Play()
	tween.Completed:Wait()
	local ahhhhtween = tweenservice:Create(primary,TweenInfo.new(15, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {CFrame = primary.CFrame + Vector3.new(0,-2000,0)})
	ahhhhtween:Play()
end

primary.Touched:Connect(function(hit)
	print(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local players = game.Players:GetPlayers()
		local touchedPlayers = {} -- Table to track players
		local count = 0
			--new
		for _, player in pairs(players) do
			player.Character.HumanoidRootPart.Touched:Connect(function(e)
				if e == primary then
					if not touchedPlayers[player.UserId] then
						touchedPlayers[player.UserId] = true 
						count += 1
						player.Character.IsInElevator.Value = true
						if player.Character.IsInElevator.Value == true then 
							local weld = Instance.new("WeldConstraint")
							hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
							weld.Parent = model
							weld.Part0 = hit.Parent.HumanoidRootPart
							weld.Part1 = primary
						end
					end
				end
				if count == #players then
					if not debounce then
						debounce = true
						task.wait(0.3)
						ending()
						event:FireClient(player, model) --this one
					end
				end
			end)
		end
			--old
		--[[for _, player in pairs(players) do
			player.Character.HumanoidRootPart.Touched:Connect(function(e)
				if e == primary then
					count += 1
					player.Character.IsInElevator.Value = true
					if player.Character.IsInElevator.Value == true then 
						local weld = Instance.new("WeldConstraint")
						hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
						weld.Parent = model
						weld.Part0 = hit.Parent.HumanoidRootPart
						weld.Part1 = primary
					end
				end
				-- Check if all players are touching the primary object
				if count == #players then
					if player.Character.IsInElevator.Value == true then
						if not debounce then
							debounce = true
							local anim = Instance.new("Animation")
							anim.AnimationId = "rbxassetid://103845461454072"
							local rahhhh = hit.Parent.Humanoid:LoadAnimation(anim)
							rahhhh:Play()
							hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
							task.wait(0.3)
							print("players: "..#players)
							print("count: "..count)
							ending()
							task.wait(13.8)
							player.PlayerGui.DeadGui.Enabled = true
						end
					end
				end
			end)
		end]]
	end
end)

oh this is a serverscript. thats all. If you want to play anims, just fire the client of the player. Or you can do it in a localscript and fire a remote event to the server. then the server gets the playerid and does the check.

This is pretty simple, just make a table and store all players that are currently touching the part in it. If the amout of objects in the table are equal to the numer of people in the server run whatever code you want.

Example:

local part = script.Parent
local players = game:GetService("Players")
local playersTouching = {}
local canRun = true

function mainCode ()
	--your code
end

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and table.find(playersTouching, hit.Parent.Name) == nil then
		table.insert(playersTouching, hit.Parent.Name)
		if #playersTouching == #players:GetChildren() and canRun == true then
			canRun = false
			mainCode()
		end
	end
end)


part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and table.find(playersTouching, hit.Parent.Name) then
		table.remove(playersTouching, table.find(playersTouching, hit.Parent.Name))
	end
end)

This code also makes it so it can only be triggered once, since you’re doing a cutscene.
Put this script into the part that the players are supposed to touch.

Edit: If this doesn’t work for some reason just tell me and I’ll try to help you, if it does work make sure to mark this as solution for other people on the DevForum.
Edit 2: Just realized I didn’t call :Touched() on the “part” instance but on the script’s parent, I changed it so it’s easier to actually define the part you want the players to touch.

I only want for when the player touched the part. Not when the player is touching the part.

But I fired the event to the player and it didn’t work. So should I change the player variable, or should I fire it from a local script?

can you tell me exactly whats wrong? What you can do is just use this as a serverscript and then fireclient() to the player that’s inside to play the anim. The easier thing is to make a localscript that just detects the touch, plays the anim, and then fires the server to do the check also sending the player to the server. then the server does the check with the different players.

The problem is that the remote is not firing to the local script that plays the anim.

what how can i have code


This is the elevator script

local model = script.Parent
local primary = model.PrimaryPart
local tweenservice = game:GetService("TweenService")
local debounce = false
local player = game:GetService("Players").PlayerAdded:Wait()
local event = game.ReplicatedStorage.Events:WaitForChild("ElevatorEnd")

function ending()
	workspace["PM (PlaceholderMusic)"]["Elevator Jammed"]:Play()
	task.wait(1)
	local tween = tweenservice:Create(primary,TweenInfo.new(25.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {CFrame = primary.CFrame + Vector3.new(0,-175,0)})
	tween:Play()
	tween.Completed:Wait()
	local ahhhhtween = tweenservice:Create(primary,TweenInfo.new(15, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {CFrame = primary.CFrame + Vector3.new(0,-2000,0)})
	ahhhhtween:Play()
end

primary.Touched:Connect(function(hit)
	print(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local players = game.Players:GetPlayers()
		local touchedPlayers = {} -- Table to track players
		local count = 0
			--new
		for _, player in pairs(players) do
			player.Character.HumanoidRootPart.Touched:Connect(function(e)
				if e == primary then
					if not touchedPlayers[player.UserId] then
						touchedPlayers[player.UserId] = true 
						count += 1
						player.Character.IsInElevator.Value = true
						if player.Character.IsInElevator.Value == true then 
							local weld = Instance.new("WeldConstraint")
							hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
							weld.Parent = model
							weld.Part0 = hit.Parent.HumanoidRootPart
							weld.Part1 = primary
						end
					end
				end
				if count == #players then
					if not debounce then
						debounce = true
						task.wait(0.3)
						ending()
						event:FireClient(player, model)
					end
				end
			end)
		end
			--old
		--[[for _, player in pairs(players) do
			player.Character.HumanoidRootPart.Touched:Connect(function(e)
				if e == primary then
					count += 1
					player.Character.IsInElevator.Value = true
					if player.Character.IsInElevator.Value == true then 
						local weld = Instance.new("WeldConstraint")
						hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
						weld.Parent = model
						weld.Part0 = hit.Parent.HumanoidRootPart
						weld.Part1 = primary
					end
				end
				-- Check if all players are touching the primary object
				if count == #players then
					if player.Character.IsInElevator.Value == true then
						if not debounce then
							debounce = true
							local anim = Instance.new("Animation")
							anim.AnimationId = "rbxassetid://103845461454072"
							local rahhhh = hit.Parent.Humanoid:LoadAnimation(anim)
							rahhhh:Play()
							hit.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
							task.wait(0.3)
							print("players: "..#players)
							print("count: "..count)
							ending()
							task.wait(13.8)
							player.PlayerGui.DeadGui.Enabled = true
						end
					end
				end
			end)
		end]]
	end
end)

And this is the local script in starter character

local event = game.ReplicatedStorage.Events:WaitForChild("ElevatorEnd")

event.OnClientEvent:Connect(function(model)
	print("im here!")
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://103845461454072"
	local rahhhh = script.Parent.Humanoid:LoadAnimation(anim)
	rahhhh:Play()
	script.Parent.HumanoidRootPart.CFrame = model.tppart.CFrame
	task.wait(13.8)
	game.Players.LocalPlayer.PlayerGui.DeadGui.Enabled = true
end)

does it print im here?

yeah for a test on if it works

get rid of script.Parent.Humanoid:LoadAnimation(anim)

and try jsut using game.players.localplayer

It doesn’t work. And I added the :Play() and it didn’t play. And the print doesn’t print out the message. I think the remote is just not firing. This was the result in output.

  18:46:30.853  RightHand  -  Server - Main:19
  18:46:30.968  RightUpperArm  -  Server - Main:19
  18:46:30.971  LeftLowerLeg  -  Server - Main:19
  18:46:30.974  LeftFoot  -  Server - Main:19
  18:46:30.976  RightLowerArm  -  Server - Main:19
  18:46:31.087  LeftUpperLeg  -  Server - Main:19
  18:46:31.092  LeftUpperLeg  -  Server - Main:19
  18:46:31.095  LeftLowerArm  -  Server - Main:19
  18:46:31.097  Head  -  Server - Main:19
  18:46:31.101  Head  -  Server - Main:19
  18:46:31.104  UpperTorso  -  Server - Main:19
  18:46:31.107  RightLowerLeg  -  Server - Main:19
  18:46:31.110  LeftUpperArm  -  Server - Main:19
  18:46:31.112  LowerTorso  -  Server - Main:19
  18:46:31.115  RightHand  -  Server - Main:19
  18:46:31.118  RightUpperArm  -  Server - Main:19
  18:46:31.121  HumanoidRootPart  -  Server - Main:19
  18:46:31.123  RightUpperLeg  -  Server - Main:19
  18:46:31.126  LeftHand  -  Server - Main:19
  18:46:31.129  Handle  -  Server - Main:19
  18:46:31.131  Handle  -  Server - Main:19
  18:46:31.133  Handle  -  Server - Main:19
  18:46:31.135  Handle  -  Server - Main:19
  18:46:31.138  LeftLowerLeg  -  Server - Main:19
  18:46:31.140  LeftFoot  -  Server - Main:19
  18:46:31.142  RightLowerArm  -  Server - Main:19

nevermind i know what the problem was now
i had to switch the fireclient and ending function. maybe because after the ending is completed, it would fire. But let me try 2 players now.

edit:
its the same result. only 1 person plays the animation. also the time for the black screen was not adjusted correctly in this video

Then just remove the TouchEnded function like this:

local part = script.Parent
local players = game:GetService("Players")
local playersTouching = {}
local canRun = true

function mainCode ()
	--your code
end

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and table.find(playersTouching, hit.Parent.Name) == nil then
		table.insert(playersTouching, hit.Parent.Name)
		if #playersTouching == #players:GetChildren() and canRun == true then
			canRun = false
			mainCode()
		end
	end
end)
1 Like

Okay, I could try that since he didn’t respond yet.

Yeah but make sure you also put the beginning of the script, only remove the TouchEnded() function

It kinda works. All I need is to get the player from the character to fire all clients for the animation to play.

edit: nevermind