Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script only teleports one player?

Hi, i'm making a script that has to teleport all players to a location but it only teleports the first player joined.

		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(67.75, 14.75, 0.75)
				character:WaitForChild("Humanoid").Sit = false
				
				local workspace = game:GetService("Workspace")
				workspace.Game.House.NightInvisibleWalls.Bedroom.CanCollide = true
				
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Sleep well!"
				
				wait(15)
				
				local lighting = game:GetService("Lighting")
				lighting.ClockTime = 24
				lighting.FogEnd = 60
				
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
				character.HumanoidRootPart.Anchored = true
				
				workspace.Game.Sounds.Music:Stop()
				workspace.Game.Sounds.Night:Play()
				
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
				character.HumanoidRootPart.Anchored = true
				for i,player in pairs(game:GetService("Players"):GetChildren()) do
					local character = player.Character
					if character:WaitForChild("IsInRound").Value == true then
						wait(5)
						player.PlayerGui.TextGui.Frame.TextLabel.Text = ""
						local workspace = game:GetService("Workspace")
						wait(15)
						workspace.Game.House.NightSounds.Whispers.Sound:Play()
					end
				end
			end
		end

Full script:

db = true
while wait(1) do
	if db == true then
	if game:GetService("Workspace").Game.PlayerCount.Value >= 1 then db = false
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Welcome to Snowed in!"
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Giving players 1 minute to join."
			end
		end
		wait(60)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Players are chosen, nobody can enter the round anymore."
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "The night will approach shortly."
			end
		end
		wait(60)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Everyone goes to their beds, the night is here."
				
				local lighting = game:GetService("Lighting")
				lighting.ClockTime = 24
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				character:WaitForChild("Humanoid").Jump = true
				character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(67.75, 14.75, 0.75)
				
				local workspace = game:GetService("Workspace")
				workspace.Game.House.NightInvisibleWalls.Bedroom.CanCollide = true
				
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Sleep well!"
				
				wait(15)
				
				local lighting = game:GetService("Lighting")
				lighting.ClockTime = 24
				lighting.FogEnd = 60
				
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
				character.HumanoidRootPart.Anchored = true
				
				workspace.Game.Sounds.Music:Stop()
				workspace.Game.Sounds.Night:Play()
				
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
				character.HumanoidRootPart.Anchored = true
				for i,player in pairs(game:GetService("Players"):GetChildren()) do
					local character = player.Character
					if character:WaitForChild("IsInRound").Value == true then
						wait(5)
						player.PlayerGui.TextGui.Frame.TextLabel.Text = ""
						local workspace = game:GetService("Workspace")
						wait(15)
						workspace.Game.House.NightSounds.Whispers.Sound:Play()
					end
				end
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "ZzZz, h-huh? w-what whas t-that sound?"
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 1
				character.HumanoidRootPart.Anchored = false
				local serverstorage = game:GetService("ServerStorage")	
				local stairsmonster = serverstorage.StairsMonster:Clone()
				stairsmonster.Parent = game:GetService("Workspace")
				wait(10)
				stairsmonster:Remove()
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Probably nothing, lets sleep further."
				wait(5)
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
				character.HumanoidRootPart.Anchored = true
				player.PlayerGui.TextGui.Frame.TextLabel.Text = ""
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "A new day has begun, everyone go to breakfast."
				player.PlayerGui.TextGui.Frame.BackgroundTransparency = 1
				character.HumanoidRootPart.Anchored = false
				
				local workspace = game:GetService("Workspace")
				workspace.Game.House.NightInvisibleWalls.Bedroom.CanCollide = false
				
				workspace.Game.Sounds.Music:Play()
				workspace.Game.Sounds.Night:Stop()
				
				local lighting = game:GetService("Lighting")
				lighting.ClockTime = 12
				lighting.FogEnd = 100000
			end
		end
		wait(15)
		for i,player in pairs(game:GetService("Players"):GetChildren()) do
			local character = player.Character
			if character:WaitForChild("IsInRound").Value == true then
				player.PlayerGui.TextGui.Frame.TextLabel.Text = "Eat well!"
				character:WaitForChild("Humanoid").Jump = true
				character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(1.75, 3, 0)
				
				local workspace = game:GetService("Workspace")
				workspace.Game.House.DayInvisibleWalls.MainRoom.CanCollide = true			
				
				local serverstorage = game:GetService("ServerStorage")	
				local food = serverstorage.Food:Clone()
				food.Parent = game:GetService("Workspace")
				
				wait(5)
				player.PlayerGui.TextGui.Frame.TextLabel.Text = ""
				
				wait(55)
					food:Remove()
				end
			end
		end
	end
end

4 comments

0

That's because the script runs when the first player joins

0

It is a script in serverscriptservice with a debounce.

0

You are running a loop that iterates through the children of players to teleport players, however a :GetChildren() loop doesn't apply a code to all of them at the same time, it goes through each 1 by 1 and runs the code on that child. You are waiting 5 and then 55 seconds inside that loop, so it will be 60 seconds inbetween each player teleport

0

Is there another way to do it?

1 answer

Well that's because the script will run when the first player joins, you need to use a PlayerAdded event to fire it every time a new player joins the game

Here's my adjustments:

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character
    if character:WaitForChild("IsInRound").Value == true then
        character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(67.75, 14.75, 0.75)
        character:WaitForChild("Humanoid").Sit = false

        local workspace = game:GetService("Workspace")
        workspace.Game.House.NightInvisibleWalls.Bedroom.CanCollide = true

        player.PlayerGui.TextGui.Frame.TextLabel.Text = "Sleep well!"

        wait(15)

        local lighting = game:GetService("Lighting")
        lighting.ClockTime = 24
        lighting.FogEnd = 60

        player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
        character.HumanoidRootPart.Anchored = true

        workspace.Game.Sounds.Music:Stop()
        workspace.Game.Sounds.Night:Play()

        player.PlayerGui.TextGui.Frame.BackgroundTransparency = 0
        character.HumanoidRootPart.Anchored = true
        for i,player in pairs(game:GetService("Players"):GetChildren()) do
            local character = player.Character
            if character:WaitForChild("IsInRound").Value == true then
                wait(5)
                player.PlayerGui.TextGui.Frame.TextLabel.Text = ""
                local workspace = game:GetService("Workspace")
                wait(15)
                workspace.Game.House.NightSounds.Whispers.Sound:Play()
            end
        end
    end
end)

1 comment

0

I've put the full script as a edit.

This discussion is preserved as it was. New answers, comments, and votes are closed.