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

Script breaks and doesn't work when run again after no subjects are found in a for,do statement?

Here is my code:

local seconds = game.ReplicatedStorage.Seconds
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = Instance.new("RemoteEvent", ReplicatedStorage)
event.Name = "EndRound"
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

local function EndRoundNOW()
	for i,v in pairs(Players:GetPlayers()) do
	    if v.Team == Teams["Playing"] then
	        pcall(function()
	            v.Character:BreakJoints()
	        end)
	    end
	end
	print("someone wants to end the round")
	seconds.Value = 0
end
 
event.OnServerEvent:Connect(EndRoundNOW)

When no one is inside of the "Playing" team, the script breaks and doesn't work again once the GUI is clicked.

Here is the script that fires the event:

local debounce = false
local seconds = game.ReplicatedStorage.Seconds
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("EndRound")
function leftClick()
	if debounce == false then
		if seconds.Value > 0 then
		print("Left mouse clicked cool kid's end round button")
		debounce = true
		event:FireServer()
		script.Parent.Text = "(15)"
		wait(1)
		script.Parent.Text = "(14)"
		wait(1)
		script.Parent.Text = "(13)"
		wait(1)
		script.Parent.Text = "(12)"
		wait(1)
		script.Parent.Text = "(11)"
		wait(1)
		script.Parent.Text = "(10)"
		wait(1)
		script.Parent.Text = "(9)"
		wait(1)
		script.Parent.Text = "(8)"
		wait(1)
		script.Parent.Text = "(7)"
		wait(1)
		script.Parent.Text = "(6)"
		wait(1)
		script.Parent.Text = "(5)"
		wait(1)
		script.Parent.Text = "(4)"
		wait(1)
		script.Parent.Text = "(3)"
		wait(1)
		script.Parent.Text = "(2)"
		wait(1)
		script.Parent.Text = "(1)"
		wait(1) 
		script.Parent.Text = "End Round"
		debounce = false
		else script.Parent.Text = "bruh round is already over"
			debounce = true
			wait(1)
			debounce = false
			script.Parent.Text = "End Round"
		end
	end
end
script.Parent.MouseButton1Click:Connect(leftClick)

1 answer

Here is my very awesome and cool rewrite (totally), tell me if it works!

-- First Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Seconds = ReplicatedStorage:FindFirstChild("Seconds")

repeat
	wait()
until Seconds ~= nil

local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Name = ("EndRound");
RemoteEvent.Parent = ReplicatedStorage

RemoteEvent.OnServerEvent:Connect(function(PlayerWhoCalledTheEvent)
	for _,Player in pairs(Players:GetPlayers()) do
		if Player.Team == Teams["Playing"] then 
			pcall(function() Player:LoadCharacter() end)
		end
	end
	print("The round has ended...");
	Seconds.Value = 0
end)

-- Second Script

local GuiButton = (script.Parent:IsA("GuiButton") and script.Parent) or script:FindFirstAncestorOfClass("GuiButton")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DebounceHit = false
local RemoteEvent = ReplicatedStorage:FindFirstChild("EndRound")

repeat
	wait()
until RemoteEvent ~= nil

local function MouseClickFunction()
	if DebounceHit ~= false then
		return
	end
	DebounceHit = true
	if Seconds.Value > 0 then
		print("Left mouse clicked Cool Kid's End Round Button");
		RemoteEvent:FireServer()
		for i = 1,15 do
			GuiButton.Text = tostring(15 - i)
			wait(1)
		end
		GuiButton.Text = ("End Round");
	else
		warn("The round has already ended? Lol.");
		return	
	end
end

4 comments

0

Sorry, it didn't work. When I press the button, nothing happens.

0

There are also no errors in the output.

0

I believe this has something to do with the game, not the script. If you would like you can friend request me on roblox and maybe I can personally help you with the game?

0

Alr.

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