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

Why is the terminal script not counting down?

Hello there, I was making a terminal script for a group. While making the script I encountered issues that I can't solve them. The script is a NormalScript and It is Inside a model called as RaidSystem whos Parent is the Workspace. Inside the model is a part called as RaidSystemV1 (which is the beam), and finally inside the part the script is there named as Script. The code is given below:

timeuntilwin = 1200
totaltime = 1200

raidteamcolor = "Bright red" 
defendteamcolor = "Bright orange" 
h = Instance.new("Hint")
h.Parent = game.Workspace
h.Text = "No raid is going on currently." 

function OnTouched(hit) 
    if hit.Parent:findFirstChild("Humanoid") ~= nil then 
        if game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor == BrickColor.new(raidteamcolor) then 
                script.Parent.BrickColor = BrickColor.new(raidteamcolor) 
      wait(-1)
      timeuntilwin = totaltime
        end 

        if game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor == BrickColor.new(defendteamcolor) then
          script.Parent.BrickColor = BrickColor.new(defendteamcolor)
      wait(-1)
      timeuntilwin = totaltime
        end
	end
end

script.Parent.Touched:connect(OnTouched) 

while true do 
	wait()
	if script.Parent.BrickColor == BrickColor.new(raidteamcolor) then 
  h.Text = "Raiders win in".." "..timeuntilwin 

  if timeuntilwin <= totaltime  and timeuntilwin > 0 then 
		wait(1) 
    timeuntilwin = timeuntilwin - 1 
  end

  if timeuntilwin == 0 then 
  	h:remove() 
		winmsg = Instance.new("Message") 
    winmsg.Parent = game.Workspace 
    winmsg.Text = "Raiders have won!" 
    wait(5) 
    winmsg:remove()

    script.Parent.BrickColor = BrickColor.new(defendteamcolor) 
    timeuntilwin = totaltime
    end 
  end 
  
  if script.Parent.BrickColor == BrickColor.new(defendteamcolor) then 
    h.Text = "Defenders win in "..timeuntilwin
    if timeuntilwin <=totaltime and timeuntilwin > 0 then
    	wait (1)
      timeuntilwin = timeuntilwin - 1
      if timeuntilwin == 0 then
      	h.Text = "Defenders have won!"
      end
    end
  end 
end

[1] The problem I have is that whenever the defending team (Bright Orange team) touches the beam, The Hint message just shows "Defenders will win in 1200". But on the other hand when Raider team (Bright Red team) touches then the Hint message starts to countdown while the defenders hint message does not.

[2] The second problem is that when the Defender touches the beam first and the raiders then touch it, The timer for the raiders starts from 1192 when its supposed to start at 1200.

I am really confused what I have done. If you guys could lend me a hand and help me I would appreciate it. Thank you taking your time into reading this and helping me. :)

1 answer

✓ Accepted answer

I took the time to test your script in Studio.. Yeah I stopped being lazy. I fixed your first problem by implementing a new timer which we worked longer than I'd like to admit on.

I fixed your second problem also by implementing a new timer. After working several days on your script along side you.. I also implemented a few functions to organize your code a bit.. as well as to rid it of duplicates.

I fixed various bugs as well that the new timer caused. The biggest change besides the timer would be the timerset variable.. it should be self explanatory. I also fixed the issue that you asked about, where if the same team member touched the brick then the timer would reset that's also self explanatory. As well as re-implement features of your script that the new timer didn't support. You can probably figure out how the new timer is implemented by looking at it. Though I'm not the original creator of the timer.

If you have anymore questions feel free to let me know. Or post another question on here and someone will help you.

timeuntilwin = 1200
totaltime = 1200
timerset = false
h = Instance.new("Hint")
h.Parent = game.Workspace
h.Text = "No raid is going on currently." 
raidteamcolor = "Bright red" -- Raiding team color
defendteamcolor = "Bright orange" -- Defending team (i.e. Your group) color
neutralcolor = "Black" -- Neutral color [i.e. No team captured the point yet.]
script.Parent.BrickColor = BrickColor.new(neutralcolor) -- When game start's make the brick neutral

function showHint()
	--[[ This function makes the hint show again after it is removed for the win message.
	 Timer was still countine while the hint wasn't showing. ]]--
	h = Instance.new("Hint")
	h.Parent = game.Workspace
	h.Text = "No raid is going on currently." 
end

-- Added an OnVictory Function To deal with showing the win message.
function OnVictory(winner)
	timerset = false -- Keeps the timer from counting down again. Also destroy's exisitng timers.
	h:remove() 
    local winmsg = Instance.new("Message") 
    winmsg.Parent = game.Workspace 
	
	if(winner == "Raiders") then
		winmsg.Text = "Raiders have won!"
	else
		winmsg.Text = "Defenders have won!"
	end
    timeuntilwin = totaltime -- Fixed a bug where timer would reset after a victory
	script.Parent.BrickColor = BrickColor.new(neutralcolor) -- Resets the beam back to "neutralcolor" [i.e. when no team have captured it]
    wait(5)
    winmsg:remove()
	showHint()
end

function DebugTimer(message)
		print(message .. "Timer is currently: " .. timeuntilwin)
end


function Timer(Time, Team) -- Timer function [Controls the countdown]		
	for i = Time, 0, -1 do
		wait(1)
    	local Minutes = math.floor(i/60)
    	local Seconds = i - (60 * Minutes)
    	
		if(timerset == false) then
			break
		end
		
		if(Team == "Raiders") then
      		h.Text = ("Raiders win in ".."d:d"):format(Minutes, Seconds)   --First text is 'minutes' and last is 'seconds'.
    	end
   
    	if(Team == "Defenders") then
    	   	h.Text = ("Defenders win in ".. "d:d"):format(Minutes, Seconds)   --First text is 'minutes' and last is 'seconds'.
    	end
    	timeuntilwin = i
		DebugTimer("Decrementing ")
    end
end

function OnTouched(hit) 
    if hit.Parent:findFirstChild("Humanoid") ~= nil then 
        if game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor == BrickColor.new(raidteamcolor) then 
        	if script.Parent.BrickColor ~= game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor then
				script.Parent.BrickColor = BrickColor.new(raidteamcolor)
      			timerset = false
			end
        end 

        if game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor == BrickColor.new(defendteamcolor) then
          	if script.Parent.BrickColor ~= game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor then
				script.Parent.BrickColor = BrickColor.new(defendteamcolor)
				timerset = false
			end
        end
    end
end

script.Parent.Touched:connect(OnTouched) 

while true do 
  wait()
  if script.Parent.BrickColor == BrickColor.new(raidteamcolor) then 
  	--h.Text = "Raiders win in".." "..timeuntilwin 
    
    if timeuntilwin == 0 then 
    	OnVictory("Raiders") -- The Raiders Won!
    	wait(1)
    end 
  	Timer(totaltime, "Raiders")
	timerset = true
  end 

  if script.Parent.BrickColor == BrickColor.new(defendteamcolor) then 
    --h.Text = "Defenders win in "..timeuntilwin
    if timeuntilwin <=totaltime and timeuntilwin > 0 then
      Timer(totaltime, "Defenders")
	  timerset = true
    	if timeuntilwin == 0 then
			OnVictory("Defenders")
			wait(1)
    	  script.Parent.BrickColor = BrickColor.new(neutralcolor) -- Resets the beam back to "neutralcolor" [i.e. when no team have captured it]
    	end
    end
  end 
end

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