Problem teleporting the third player

  1. What do you want to achieve?

i want so if there are more than 2 player it should teleport the other players on the map and change their team

  1. What is the issue?

well as the title says it wont teleport the third player only the first two players

  1. What solutions have you tried so far?

nothing that i could think of.

local roundsystemmodule = require(game.ServerScriptService:WaitForChild("ModuleScripts"):WaitForChild("roundsystemodule"))
local votingmapmmodule = require(game.ServerScriptService:WaitForChild("ModuleScripts"):WaitForChild("votingmapmodule"))
local RunService = game:GetService("RunService")

local Waitingonplayers = game.ReplicatedStorage:WaitForChild("WaitingForplayersRemoteEvent")

local roundrunning = game.ReplicatedStorage:WaitForChild("roundrunning")
roundrunning.Value = true

local killerleft = game.ReplicatedStorage:WaitForChild("killerleft")
killerleft.Value = false

local survivorleft = game.ReplicatedStorage:WaitForChild("survivorleft")
survivorleft.Value = false

local currentkiller = game.ReplicatedStorage:WaitForChild("currentkiller")
local currentsurvivor = game.ReplicatedStorage:WaitForChild("currentsurvivor")

local function checkplayers()
	local players = game.Players
	local playertable = players:GetChildren()
	for _, player in pairs(playertable) do
		print(player.Name)
	end
	local enoughplayer = #playertable
	if enoughplayer >= 2 then
		Waitingonplayers:FireAllClients(roundrunning)
		print("enough players")
		return playertable , enoughplayer
	else
		Waitingonplayers:FireAllClients()
		print("not enough players")
		return playertable , enoughplayer
	end
end


game.Players.PlayerAdded:Connect(function(player)
	if roundrunning.Value == false then
		checkplayers()
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	if currentkiller.Value == player then
		killerleft.Value = true
		print("killerleft")
		checkplayers()
		return
	end
	if currentsurvivor.Value == player then
		survivorleft.Value = true
		print("survivor left")
		checkplayers()
		return
	end
	checkplayers()
end)

while true do
	task.wait(1)
	local playertable, enoughplayers = checkplayers()
	
	if enoughplayers >= 2 and roundrunning.Value == true then
		roundrunning.Value = false
		
		print("BEFORE VOTING:", #playertable)

		local winningmap = votingmapmmodule.VotingSystem(playertable)

		print("AFTER VOTING:", #playertable)

		roundsystemmodule.Roundsystem(playertable, winningmap)
	else
		print("no round")
	end
end

this is my server script i added prints, print(player.Name) it prints player1, player2 and player3 which should be three players in total right? but print(“AFTER VOTING:”, #playertable) it prints 2? and somehow print(“BEFORE VOTING:”, #playertable) doesn’t print at all, ive looked inside my module to see if i deleted or decreased the playertable or something

local module = {}

local maps = game:WaitForChild("ReplicatedStorage"):WaitForChild("Maps")
local tvmaps = game:WaitForChild("Workspace"):WaitForChild("ShowMapsTVS")

local killer = game:GetService("Teams").Killer
local survivor = game:GetService("Teams").Survivors

local votingsystemRemotevent = game:WaitForChild("ReplicatedStorage"):WaitForChild("votingsystemremotevent")
local votingmapandvotesRemotevent = game:WaitForChild("ReplicatedStorage"):WaitForChild("votingmapandvotesremotevent")
local tvguiremotevent = game:WaitForChild("ReplicatedStorage"):WaitForChild("TVGUI'sRemoteEvent")

local votingpad1 = game.Workspace:WaitForChild("VotingPad1")
local votingpad2 = game.Workspace:WaitForChild("VotingPad2")
local votingpad3 = game.Workspace:WaitForChild("VotingPad3")

local votingpadsdiscconect1 = nil
local votingpadsdiscconect2 = nil
local votingpadsdiscconect3 = nil

local roundrunning = game.ReplicatedStorage:WaitForChild("roundrunning")

function module.VotingSystem(playertable)
	if roundrunning.Value == false then
		local enoughplayers = #playertable
		local highestnumber = 0
		local winningmap = ""

		local votes = {}

		local mapvotes = {
			Basement = 0,
			Cellar = 0,
			Caves = 0 
		}
		
		local randommaps = {
			"Basement",
			"Cellar",
			"Caves"
		}

		votingpadsdiscconect1 = votingpad1.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local playervote = votes[player]
				playervote = "Basement"
				tvguiremotevent:FireAllClients(mapvotes)
			end
		end)

		votingpadsdiscconect2 = votingpad2.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local playervote = votes[player]
				playervote[player] = "Cellar"
			end
		end)

		votingpadsdiscconect3 = votingpad3.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local playervote = votes[player]
				playervote[player] = "Caves"
			end
		end)

		for i = 25, 0, -1 do
			if enoughplayers <= 1 then
				print("yoyoyoyoyoy")
				break
			else
				print(i)
				task.wait(1)
				votingsystemRemotevent:FireAllClients(i)
				if i <= 0 then
					break
				end
			end
		end

		for _, vote in pairs(votes) do
			if vote == "Basement" then
				mapvotes.Basement += 1
			elseif vote == "Cellar" then
				mapvotes.Cellar += 1
			elseif vote == "Caves" then
				mapvotes.Caves += 1
			end
		end

		for map, voteded in pairs(mapvotes) do
			if voteded > highestnumber then
				highestnumber = voteded
				winningmap = map
				votingmapandvotesRemotevent:FireAllClients(highestnumber, winningmap)
				for _, sectionmap in pairs(maps:GetChildren()) do
					if winningmap == "Basement" then
						if sectionmap.Name == "Basement" then
							print("it parent basement")
						end
					if winningmap == "Cellar" then
						if sectionmap.Name == "Cellar" then
							print("it parent cellar")
						end
					if winningmap == "Caves" then
						if sectionmap.Name == "Caves" then
							print("it parent caves")
						end
					end
				end
			end
		end
	end
end
		if winningmap == "" then
			local randommap = math.random(1, #randommaps)
			winningmap = randommaps[randommap]
			votingmapandvotesRemotevent:FireAllClients(highestnumber, winningmap)
		end

		return winningmap
	else
		return
	end
end

return module

nothing, and inside the roundsystem module i have a print(#playertable) and it prints 2, i dont know what to do, im a beginner scripter, first time doing this kinds of systems so any help is greatly appreciated, if you want photos or something else, please reply and ill reply to you shortly.

4 Likes

Seems like what you want and what the code block is asking has minor contradiction that might worth checking, you said “more than 2 player” yet used “enoughplayers >= 2” instead of “enoughplayers > 2” they’re different and “>=” means “if enoughplayers is more than or equal to 2 then run the code block inside if statement”. Let me know if this isn’t the case and I might come back again and analyze your script deeper if I got any time.

PS: If it didn’t help, you should answer these questions so I and others can help you easier. Is your teleportation code block using playertable to determine who it teleports? I couldn’t find the code block that triggers teleportation or sets the players position, if its in the scripts you showed I would like you to describe where its happening in the scripts and I would like you to show the script that sets the players position.

3 Likes

okay, now that ive changed that it pritns 3 which is good,

local module = {}

local killer = game.Teams.Killer
local survivor = game.Teams.Survivors
local lobby = game.Teams.Lobby

local DataStoreService = game:GetService("DataStoreService")
local PlayerInventory = DataStoreService:GetDataStore("PlayerInventory")
local PlayerTears = DataStoreService:GetDataStore("PlayerInventory", "Tears")

local sendchoosenplayer = game.ReplicatedStorage:WaitForChild("SendChosenPlayerRemoteEvent")
local SurvivorAndTimerRemoveEvent = game.ReplicatedStorage:WaitForChild("SurvivorAndTimerRemoveEvent")
local SurvivorRemoveEvent = game.ReplicatedStorage:WaitForChild("SurvivorRemoteEvent")
local KillerRemoveEvent = game.ReplicatedStorage:WaitForChild("KillerRemoteEvent")
local PlayersGUIRemoteEvent = game.ReplicatedStorage:WaitForChild("PlayersGUIRemoteEvent")
local heartremotevent = game.ReplicatedStorage:WaitForChild("HeartRemoteEvent")
local makeisaacguienabledremotevent = game.ReplicatedStorage:WaitForChild("MakeIsaacGUIEnabledRemotEvent")
local sendtearsremotevent = game.ReplicatedStorage:WaitForChild("SendTearsRemoteEvent")


local isaacmodelskin = game.ServerStorage.Skins.Isaac.StarterCharacter

local roundrunning = game.ReplicatedStorage:WaitForChild("roundrunning")
local killerleft = game.ReplicatedStorage:WaitForChild("killerleft")
local currentkiller = game.ReplicatedStorage:WaitForChild("currentkiller")
local survivorleft = game.ReplicatedStorage:WaitForChild("survivorleft")
local currentsurvivor = game.ReplicatedStorage:WaitForChild("currentsurvivor")

local maps = game.ReplicatedStorage:WaitForChild("Maps")

local isaacspawner = game.Workspace:WaitForChild("IsaacPlaceHolderSpawner")

function module.Roundsystem(playerstable, winningmap)
	local isready = false
	local guifalseortrue = true
	local guifalseortruesurvivors = true
	local isaacgui = false
	local foundkiller = false
	local foundsurvivor = false
	
	roundrunning.Value = false
	
	
	local currenttime = 250
	local survivorcount = 0
	
	local survivortabel = {}

	local function checksurvivors()
		survivorcount = 0

		for _, player in pairs(playerstable) do
			if player.Team == survivor then
				survivorcount += 1
			end
		end
	end
	
	local function checkmapandteleportended()
			local wonmap = workspace:WaitForChild(winningmap)
			wonmap.Parent = game.ReplicatedStorage.Maps
		end
	
	
	local discconectdeath = nil

	local chosenIndex = math.random(1, #playerstable)
	local player = playerstable[chosenIndex]

	local character = isaacmodelskin:Clone()
	local isaachumanoidrootpart = character:WaitForChild("HumanoidRootPart")
	character.Name = player.Name
	isaachumanoidrootpart.CFrame = player.Character:WaitForChild("HumanoidRootPart").CFrame
	

	player.Team = killer
	currentkiller.Value = player

	player.Character = character
	character.Parent = workspace

	local humanoid = character:WaitForChild("Humanoid")
	local root = character:WaitForChild("HumanoidRootPart")
	
	local function checkmapandteleport()
		local wonmap = maps:WaitForChild(winningmap)
		wonmap.Parent = workspace
		local teleporter = wonmap:WaitForChild("TeleportPlayersPartBasement")

		for _, teleportplayers in pairs(playerstable) do
			if teleportplayers.Team == survivor then
				local teleportplayerhrp = teleportplayers.Character:WaitForChild("HumanoidRootPart")
				teleportplayerhrp.CFrame = teleporter.CFrame
				player.Character:WaitForChild("HumanoidRootPart").CFrame = isaacspawner.CFrame
				makeisaacguienabledremotevent:FireClient(player, player, isaacgui)
				task.delay(10.5, function()
					isaacgui = true
					player.Character:WaitForChild("HumanoidRootPart").CFrame = teleporter.CFrame
					sendchoosenplayer:FireClient(player, player, guifalseortrue)
					makeisaacguienabledremotevent:FireClient(player, player, isaacgui)
				end)
			end
		end
	end

local function updatetearsurvivorwon()
	print("killer won function called")
	for _, playertears in pairs(survivortabel) do
		local tears = playertears.leaderstats.Tears
		tears.Value += 100
		sendtearsremotevent:FireClient(playertears, tears.Value)
	end
	if player.Team == killer then
		local tears = player.leaderstats.Tears
		tears.Value += 50
		sendtearsremotevent:FireClient(player, tears.Value)
	end
end

local function updatetearskillerwon()
	print("killer won function called")
	for _, playertears in pairs(survivortabel) do
		local tears = playertears.leaderstats.Tears
			tears.Value += 50
			sendtearsremotevent:FireClient(playertears, tears.Value)
	end
	if player.Team == killer then
		local tears = player.leaderstats.Tears
		tears.Value += 100
		sendtearsremotevent:FireClient(player, tears.Value)
	end
end

	repeat
		task.wait()
	until player.Character == character
		and character.Parent == workspace
		and humanoid.Parent == character
		and root.Parent == character

	task.wait(0.2)
	
	print(#playerstable)
	for _, otherplayers in pairs(playerstable) do
		if otherplayers == player then
			print("this is the killer")
		else
			otherplayers.Team = survivor
			currentsurvivor.Value = otherplayers
			table.insert(survivortabel, otherplayers)
			checkmapandteleport()
			local playerhearts =  otherplayers:FindFirstChild("Hearts")
			if playerhearts then
				PlayersGUIRemoteEvent:FireClient(otherplayers, guifalseortruesurvivors)
				otherplayers:FindFirstChild("Hearts").Value = 3
				heartremotevent:FireClient(otherplayers, playerhearts)
			else
				PlayersGUIRemoteEvent:FireClient(otherplayers, guifalseortruesurvivors)
				local Hearts = Instance.new("IntValue")
				Hearts.Parent = otherplayers
				Hearts.Value = 3
				Hearts.Name = "Hearts"
			end
		end
	end

	isready = true

	for _, survivorsteam in pairs(playerstable) do
		if survivorsteam.Team == survivor then
			local othersurvivorshumanoids = survivorsteam.Character:WaitForChild("Humanoid")
			discconectdeath = othersurvivorshumanoids.Died:Connect(function()
				survivorcount -= 1
				survivorsteam.Team = lobby
				print(survivorcount)
				SurvivorAndTimerRemoveEvent:FireAllClients(currenttime ,playerstable,survivorcount)
				survivorsteam:LoadCharacterAsync()
			end)
		end
	end

	

	if isready == true then
		for timer = 250, 0 , -1 do
			currenttime = timer
			for _, othersurvivors in pairs(playerstable) do
				local tears = othersurvivors:WaitForChild("leaderstats"):WaitForChild("Tears")
				if killerleft.Value == true then
					print("killerleft is true")
					module.EndRound(othersurvivors, player, currentkiller, foundkiller, foundsurvivor ,guifalseortrue, guifalseortruesurvivors)
					break
				end
				if survivorleft.Value == true then
					print("survivorleft is true")
					module.EndRound(othersurvivors, player, currentkiller, foundkiller, foundsurvivor ,guifalseortrue, guifalseortruesurvivors)
				end
				if othersurvivors.Team == survivor then
					checksurvivors()
					print(survivorcount)
					task.wait(1)
					SurvivorAndTimerRemoveEvent:FireAllClients(timer, playerstable, survivorcount)
					if survivorcount <= 0 then
						KillerRemoveEvent:FireAllClients()
						updatetearskillerwon()
						task.wait(5)
						module.EndRound(othersurvivors, player, currentkiller, foundkiller, foundsurvivor)
						checkmapandteleportended()
						break
					elseif timer <= 0 and survivorcount >= 1 or not player then
						SurvivorRemoveEvent:FireAllClients()
						updatetearsurvivorwon()
						task.wait(1.5)
						module.EndRound(othersurvivors, player, currentkiller, foundkiller, foundsurvivor)
						checkmapandteleportended()
						break
						end
					end
				end
			end
		end
	end

function module.EndRound(othersurvivors, player, currentkiller, foundkiller, foundsurvivor , guifalseortrue, guifalseortruesurvivors)
	local getplayers = game.Players:GetPlayers()
	
	for _, checkiller in pairs(getplayers) do
		if checkiller == currentkiller.Value then
			foundkiller = true
			guifalseortrue = false
			guifalseortruesurvivors = false
			roundrunning.Value = true
			killerleft.Value = false
		end
		if checkiller == currentsurvivor.Value then
			foundsurvivor = true
			guifalseortrue = false
			guifalseortruesurvivors = false
			roundrunning.Value = true
			survivorleft.Value = false
		end
			if othersurvivors and player then
				sendchoosenplayer:FireClient(player, player, guifalseortrue)
				PlayersGUIRemoteEvent:FireClient(othersurvivors, guifalseortruesurvivors)

				othersurvivors.Team = lobby
				player.Team = lobby
				othersurvivors:LoadCharacterAsync()
				player:LoadCharacterAsync()
			end
			if foundkiller == false then
				print("killer left module ")
				killerleft.Value = true
				if checkiller.Team == survivor then
					checkiller.Team = lobby
					checkiller:LoadCharacterAsync()
				end
			end
			if foundsurvivor == false then
				print("survivor left module")
				survivorleft.Value = true
				if checkiller.Team == killer then
					checkiller.Team = lobby
					checkiller:LoadCharacterAsync()
				end
			end
		end
	end
return module

here is my round module i get an error at line 86 local wonmap = maps:WaitForChild(winningmap) which is an infiniteyield, why? and thank you so so much, i usually use >= instead of > ill keep this mind, oops i tried playing with only 2 players and the game wont start i think that’s why i used >= so if enughplayer is greater or equal to 2 and it seem that it kinda broke the voting system too?

WaitForChild gives the infinite yield error when it waited too long for the instance to appear. Let say winning map is “Heaven” if you don’t have instance called “Heaven” in workspace, it will give you error after waiting for it long enough.

PS: Reason it might be teleporting only 2 players could be that teleport code happens when 3rd player’s client is still loading, you could delay it and give 5 more seconds in intermissions instead of instantly teleporting when you have enough players (if you check other games you can see that before starting the match game waits a little often its 1 minute countdown). As for the idea of teleporting when someone new joins in server, is flawed because if killer’s goal is to kill everyone, it now needs to kill more because newly joined player added to the match which is frustrating for the killer.

2 Likes

ur are right! i cant work right now but i will try tomorrow and keep you updated, and thank you so much you dont know how much this means, one question how do you know what to do like i didn’t think of a delay between choosing the map and the round system

I made systems that weren’t working properly because of the same reasons as yours. As you script more you will learn more things and will be able to find fixes easier. For example right now you learned why infinite yield happens, you learned difference between “>=” and “>” in future if someone has a problem caused by these things, you will be able to help them and they might even end up asking you the same question.

1 Like

i added a intermission but it wasnt that, i have a loop where it checks and makes the players from the lobby to the survivor team and i had a function checkmapandteleport() so for every player i would do the function where inside the function i did

		local wonmap = maps:WaitForChild(winningmap)
		wonmap.Parent = workspace

which would be a infinite yield, simple fix instead of calling for every player i call it once, and it seems to fix it and it works!, again thank you so much for your help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.