Log in to vote
-3

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

Asked by
TrollD3 105
7 years ago

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

Log in to vote
1
Answered by
adark 5487
7 years ago

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

Log in to vote
-1
Answered by
digpoe 65
7 years ago

Please provide code with your answers. Simply posting an explanation does not help someone new to programming understand how to implement a concept programatically.

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.

Log in to vote
-1
Answered by 7 years ago

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.

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 adark 5487 — 7y
0
Interesting... Thanks for the correction! Edited... grasheeno 70 — 7y