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

My script keeps teleporting and no errors pop up can you please help me?

So basically it keeps teleporting me to the lobby then back to the game when it is the intermission and no error pops up here is the script:

local roundLength = 120
local intermissionLength = 15
local Status = game.ReplicatedStorage.Status
local inRound = game.ReplicatedStorage.InRound
local Lobby = game.Workspace.Lobby
local Roundspawn = game.Workspace.Maps["Science Lab"].ScienceLabsSpawns.ScienceLabSpawn


inRound.Changed:Connect(function()
	wait(1)
	if inRound.Value == true then
		for _, player in pairs(game.Players:GetChildren()) do
			local char  = player.Character
			char.HumanoidRootPart.CFrame = Roundspawn.CFrame
		end	

	else
		wait(1)
		for _, player in pairs(game.Players:GetChildren()) do
			local char  = player.Character
			char.HumanoidRootPart.CFrame = Lobby.CFrame
		end

	end

end)




local function roundTimer()
	while wait() do
		for i = intermissionLength, 1, -1 do
			inRound.Value = false
			wait(1)
			Status.Value = "intermission: ".. i .." seconds left!"
		end
		for i = roundLength, 1, -1 do
			inRound.Value = true
			wait(1)
			Status.Value = "Game: ".. i .." seconds left"
		end
	end	
end

spawn(roundTimer)


****

2 comments

0

Im pretty sure it keeps teleporting you because in your roundTimer function it constantly changes the boolean, which will trigger your teleporting event for in Round.Changed()

0

but how do i fix it?

1 answer

So there are a couple of issues here. The main issue is that you are changing the value inside the for loop rather than before it, so every second it fires the :Changed() event at the top. To fix this, simply move the inRound.Value = ... above the for loop.

The next thing is that you are spawning the while loop, which is pointless. You connect the :Changed() event before the while loop, so it will run even though it is on the same thread as the while loop.

Lastly, I moved the check for the inRound value inside the for loop so you are only making one for loop rather than 2.

local replicatedStorage = game:GetService("ReplicatedStorage") --get service before using it

local Status = replicatedStorage.Status
local inRound = replicatedStorage.InRound

local Lobby = workspace.Lobby
local Roundspawn = workspace.Maps["ScienceLab"].ScienceLabsSpawns.ScienceLabSpawn

local intermissionLength = 15
local roundLength = 120

inRound.Changed:Connect(function()
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if inRound.Value then
			player.Character.HumanoidRootPart.CFrame = Roundspawn.CFrame
		else
			player.Character.HumanoidRootPart.CFrame = Lobby.CFrame
		end
	end
end)

while true do --while wait() do is bad
	inRound.Value = false

	for i = intermissionLength, 1, -1 do
		Status.Value = "intermission: " .. i .. " seconds left!"
		wait(1)
	end

	inRound.Value = true

	for i = roundLength, 1, -1 do
		Status.Value = "Game: " .. i .. " seconds left"
		wait(1)
	end
end 

Hope this helps!

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