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

Why isnt this working? Making Players stop at specific time.

This is actually a TEAM DEATH MATCH SCRIPT. The time is actually 10 MIN LONG!! How would I shorten this code and make the players stop when time = 0??!! The walkspeed should be zero i think ;-;!


while true do local h = script.Parent wait(1) h.Text = "1:00" wait(1) h.Text = "0:59" wait(1) h.Text = "0:58" wait(1) -----edited for readability h.Text = "0:03" wait(1) h.Text = "0:02" wait(1) h.Text = "0:01" wait(1) h.Text = "0:00" h:remove() end Endmatch()

function Endmatch() game.Players:GetPlayers()
game.Players.WalkSpeed = 0 end


3 answers

First, let's reduce that while loop. A countdown is a very simple loop, a numeric for:

local timeLimit = 60*10 -- 10 minutes

while true do
	local h = script.Parent
	for curTime = timeLimit, 0, -1 do
		local minutes = math.floor(timeLimit/60) -- This is integer division
		local seconds = timeLimit%60 -- The modulus operator gets the remainder

		h.Text = (minutes < 10 and "0" or "") .. minutes .. ":" .. (seconds < 10 and "0" or "") .. seconds
		wait(1)
	end
	h.Parent = nil --This is equivalent to h:Remove(). Why would you remove this *and* put it in an infinite loop?
end

Now, to make everyone stop moving, we loop through the Table returned by GetPlayers() using a generic for:

for _, player in ipairs(game.Players:GetPlayers()) do -- `_` is used for "unused variables" in Lua. It's just a placeholder so we can get the return value we actually care about: the Value of the table.
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		player.Character.Humanoid.WalkSpeed = 0
	end
end

Since the first code block runs in an infinite loop, you have to insert that WalkSpeed code into the loop for it to ever be run, like so:

local timeLimit = 60*10 -- 10 minutes

while true do
	local h = script.Parent
	for curTime = timeLimit, 0, -1 do
		local minutes = math.floor(timeLimit/60)
		local seconds = timeLimit%60

		h.Text = (minutes < 10 and "0" or "") .. minutes .. ":" .. (seconds < 10 and "0" or "") .. seconds
		wait(1)
	end
	h.Parent = nil

	for _, player in ipairs(game.Players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("Humanoid") then
			player.Character.Humanoid.WalkSpeed = 0
		end
	end
end

Firstly, you should be using for loops for something that long. You should also be using Destroy() instead of remove(). Also, it's in a "while" loop so your "Endmatch" function won't run.

Please, for your own sake, use a for loop when counting down like that. You're using a "while true do" loop, which will keep repeating forever, so you will never get on to calling the function.

2 comments

0

The note on out-of-order calling is incorrect, since the EndGame function is global. https://scriptinghelpers.org/questions/16395/what-are-the-differences-between-local-function-and-function

0

Interesting... Thanks for the correction! Edited...

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