Start When Two Players Have Hit the StartPart

  1. What do you want to achieve?
    I want the game to start when at least two players have hit/touched the start part.

  2. What is the issue? So far no matter which way I write the script it is not working properly.
    One player can teleport currently if i jump several times
    Two players can teleport if they jump several times
    Currently a player only has to jump twice to be teleported

Also oddly when testing with two players the Player Count table only seems to grab Player1 even if Player2 hits the part ? I’ve included a screen print of the output window showing that table print, even though both players hit the part it only recorded Player1.

  1. What solutions have you tried so far? I’ve tried:
    Remote Event Fire
    Various tables
    Counts with IntValue
    Teams, lobby vs players

This image depicts what happens with Player Count in test mode with two players
On this test, Player 1 hit the start and so did Player 2, but it only recognizes Player 1
SnippetofExplorer
Explorer Window of my Start Part being Hit/Touched
SnippetofStartPart


startrace = game.Workspace.Race.StartRace
local PlayerService = game:GetService("Players") 
local PlayerCount = script.Parent.PlayerCount
boxnewpos = Vector3.new(-623.995, 2.49, 108.233)
pc = {} -- used with PlayerTouchedFunction
startpad = {}
playteam = {}
local playingTeam = game.Teams.Playing
playingTeam.Name = "Playing"
local lobbyTeam = game.Teams.Lobby
player = game:GetService("Players").PlayerAdded:Wait()
player.Team = lobbyTeam

local function PlayerTouched(startrace)
	PlayerCount.Value = PlayerCount.Value + 1
	print(PlayerCount.Value)	
	table.insert(pc, player.UserId)
	player.Team = playingTeam
	local sgui = player:WaitForChild("PlayerGui"):WaitForChild("StartNameGui") 
	local txtlbl = sgui.TextLabel 
	table.insert(startpad, player.Name) 
	print(startpad, player.Name)
	table.insert(playteam,player.Team)
	print(playteam,player.Team)
end

local db = true
startrace.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false			
			PlayerTouched(startrace) 				
			local player = hit.Parent
			local PlayerCount = script.Parent.PlayerCount
			if player then	
				for i, v in pairs(playteam) do			
						
						if PlayerCount.Value >= 2 and #playteam >=2 and playingTeam.Name == "Playing" then
							wait() -- wait before tp the players in case more join- may add this in later
							box = game.Workspace.Race.box 
							box.Transparency = 0
							box.Position = boxnewpos
							local torso = player.HumanoidRootPart
							torso.CFrame = CFrame.new(-953.054, 521.008, 789.054)															
						else
							print("NeedTwo")				
						end
						end			
			end
			wait(7)
			db = true									
		end
	end		
end)

This was my remote event that was in Replicated Storage when I tried to use it and could not get it to work, so I took it out of the script I have included now because I started to test with Teams instead.
SnippetforEventinRS

This is my script in Server Script Service for when I tested the Remote Event
SnippetforSSSscriptforEvent

script in Server Script Service for firing remote event

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)	
	local StartPadNameUpdate = game.ReplicatedStorage:WaitForChild("StartPadNameUpdate")
	wait(1)
	StartPadNameUpdate:FireAllClients("StartNameGui","TextLabel","TextLabel.Text")
end)

This is my setup for the StarterGui, SurfaceGui. I tried with a script and a local script and an adorne of the gui to the start part. Its sort of working because my player.Name does show on the part when i touch it, image displays this works. I just couldn’t use it for the teleport when two players hit script portion.
SnippetforGui

Script in Starter Gui for - I tried various versions of this script as well and couldn’t get it to work
I am however successfully updating the player.Name to appear on the start part. It only shows the last player who hit the part though.

local Players = game:GetService("Players")
plr = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
fireitup =  rs:WaitForChild("StartPadNameUpdate")


local debounce = false
start.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true	
		plr = hit.Parent
		if plr then
			
				txtlbl.Text = plr.Name
			
			
			
		end	
	end
	wait()
	debounce = false
end)

u need to summarize ur post cause i dont think anyone is reading allat

You should check if the player that has already touched the part is the player that has again touched the part, if so don’t add them to the list.

if table.find( yourtable, player ) then return end

You should also be using the user object to store in the table

why not read it, folks might learn from it…

I did try table.find()… I will have to try again…

Something like this should work, don’t know if it will 100% work as this code is quite messy and hard to understand

startrace = game.Workspace.Race.StartRace
local PlayerService = game:GetService("Players") 
local PlayerCount = script.Parent.PlayerCount
boxnewpos = Vector3.new(-623.995, 2.49, 108.233)
pc = {} -- used with PlayerTouchedFunction
startpad = {}
playteam = {}

local function PlayerTouched(startrace, player)
        if table.find( pc, player.UserId ) then return end
	PlayerCount.Value = PlayerCount.Value + 1
	print(PlayerCount.Value)	
	table.insert(pc, player.UserId)
	player.Team = playingTeam
	local sgui = player:WaitForChild("PlayerGui"):WaitForChild("StartNameGui") 
	local txtlbl = sgui.TextLabel 
	table.insert(startpad, player.Name) 
	print(startpad, player.Name)
	table.insert(playteam,player.Team)
	print(playteam,player.Team)
end

local db = true
startrace.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false			
			local player = hit.Parent
			PlayerTouched(startrace, player )
			local PlayerCount = script.Parent.PlayerCount
			if player then	
				for i, v in pairs(playteam) do			
						
						if PlayerCount.Value >= 2 and #playteam >=2 and playingTeam.Name == "Playing" then
							wait() -- wait before tp the players in case more join- may add this in later
							box = game.Workspace.Race.box 
							box.Transparency = 0
							box.Position = boxnewpos
							local torso = player.HumanoidRootPart
							torso.CFrame = CFrame.new(-953.054, 521.008, 789.054)															
						else
							print("NeedTwo")				
						end
						end			
			end
			wait(7)
			db = true									
		end
	end		
end)

still helps to summarize your issue

Ok sorry its a bit messy, I probably have more in the scripts than I need right now b/c I was testing a bunch of different things. So this isn’t the cleaned up final version. :slight_smile:

Well I worked with it some more and didn’t get it working as I wanted. I’ve since changed my approach and the way I am doing the script will now be with more than one timer. The touch is just causing too may conflicting issues with my others scripts and it seems no matter which way I wrote it, a new issue arose. Thanks for your suggestions though. My new script works perfectly.

1 Like