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

What is wrong with this script?

I've written this script for basically a round system for a game, but it just doesn't function.

while true do --begin loop
	IntermissionTime = 30
	a = game.Players:GetChildren()
	x = 4 - #a
	if #a < 4 then --check for numplayers
		for i,v in pairs(a) do
			v.PlayerGui.ScreenGui.Frame.RoundTime.Text = x.." more players are needed to start the round."
		end
	elseif #a >= 4 then
		repeat --intermission timer run
		wait(1)
		for i,v in pairs(a) do --change playergui intermission text
		IntermissionTime = IntermissionTime - 1
		player.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Intermission: "..IntermissionTime
		end
		until
		IntermissionTime == 0
	end
	if IntermissionTime == 0 then --begin round
		for i,v in pairs(a) do --change playergui text to round beginning
			a.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Round beginning!"
		end
		wait(5)
		-- randomly choose 1 player
		local players = {game.Players:GetChildren()}
		local randomPlayer = math.random(1,#players)
		randomPlayer.TeamColor = BrickColor.new("Bright red")
		m = Instance.new("Message")
		m.Parent = game.Workspace
		m.Text = randomPlayer.." is the Juggernaut this round."
		wait(5)
		m:Destroy()
		
		-- spawn random map
		local maps = {"Map1", "Map2", "Map3", "Map4", "Map5"}
		local randomMap = maps[math.random(1, #maps)]
		clone = game.Lighting:FindFirstChild(randomMap):Clone()
		clone.Parent = Game.Workspace -- Make sure to set the clones parent.

		
		-- delete lobby spawnlocations
		spawns = game.Workspace.Lobby:GetChildren()
		for i,v in pairs(spawns) do --reparent spawnlocations
			if v:IsA("SpawnLocation") then
				v.Parent = game.Lighting
			end
		end
		
		--spawn players
		for i,v in pairs(a) do --kill players
			v.Character:WaitForChild("Humanoid").Health = 0
		end
		
		--begin round timer
		RoundTime = 300
		repeat
		wait(1)
		for i,v in pairs(a) do
			RoundTime = RoundTime - 1
			a.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Time Left: "..RoundTime
		end
		until
		RoundTime == 0
		
		if Roundtime == 0 then --round end
			for i,v in pairs(a) do --change playergui
				v.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Round over!"
			end
			for i,v in pairs(game.Lighting:GetChildren()) do --reassign spawnlocation parents to workspace
				if v:IsA("SpawnLocation") then
					v.Parent = game.Workspace
				end
			end
			for i,v in pairs(game.Workspace:GetChildren()) do --destroy map
				if v.Name == "Map%d" then
					v:Destroy()
				end
			end
			for i,v in pairs(a) do
				v.Character:WaitForChild("Humanoid").Health = 0
			end
			
		end --end roundend if statement
	end --end round begin	
end --end loop

I'd appreciate any help you guys can offer. Thanks!

1 answer

Please upvote my rep and/or vote this as the correct answer if this helped you or worked.

Can you describe how the script doesn't function/how it doesn't function correctly, and how it is supposed to function?

(Sorry but I can't create comments yet, I will update my answer with an actual one once I figure out how it's supposed to work)

-- Edit --

Your code may not be working because it is only choosing a random number out of the number of players in the game, not the actual player instance. Here is a fix:

while true do --begin loop
    IntermissionTime = 30
    a = game.Players:GetChildren()
    x = 4 - #a
    if #a < 4 then --check for numplayers
        for i,v in pairs(a) do
            v.PlayerGui.ScreenGui.Frame.RoundTime.Text = x.." more players are needed to start the round."
        end
    elseif #a >= 4 then
        repeat --intermission timer run
        wait(1)
        for i,v in pairs(a) do --change playergui intermission text
        IntermissionTime = IntermissionTime - 1
        player.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Intermission: "..IntermissionTime
        end
        until
        IntermissionTime == 0
    end
    if IntermissionTime == 0 then --begin round
        for i,v in pairs(a) do --change playergui text to round beginning
            a.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Round beginning!"
        end
        wait(5)
        -- randomly choose 1 player
        local players = game.Players:GetChildren()
        local randomPlayerNumber = math.random(1,#players)
	local randomPlayer = players[randomPlayer]
        randomPlayer.TeamColor = BrickColor.new("Bright red")
        m = Instance.new("Message")
        m.Parent = game.Workspace
        m.Text = randomPlayer.." is the Juggernaut this round."
        wait(5)
        m:Destroy()

        -- spawn random map
        local maps = {"Map1", "Map2", "Map3", "Map4", "Map5"}
        local randomMap = maps[math.random(1, #maps)]
        clone = game.Lighting:FindFirstChild(randomMap):Clone()
        clone.Parent = Game.Workspace -- Make sure to set the clones parent.


        -- delete lobby spawnlocations
        spawns = game.Workspace.Lobby:GetChildren()
        for i,v in pairs(spawns) do --reparent spawnlocations
            if v:IsA("SpawnLocation") then
                v.Parent = game.Lighting
            end
        end

        --spawn players
        for i,v in pairs(a) do --kill players
            v.Character:WaitForChild("Humanoid").Health = 0
        end

        --begin round timer
        RoundTime = 300
        repeat
        wait(1)
        for i,v in pairs(a) do
            RoundTime = RoundTime - 1
            a.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Time Left: "..RoundTime
        end
        until
        RoundTime == 0

        if Roundtime == 0 then --round end
            for i,v in pairs(a) do --change playergui
                v.PlayerGui.ScreenGui.Frame.RoundTime.Text = "Round over!"
            end
            for i,v in pairs(game.Lighting:GetChildren()) do --reassign spawnlocation parents to workspace
                if v:IsA("SpawnLocation") then
                    v.Parent = game.Workspace
                end
            end
            for i,v in pairs(game.Workspace:GetChildren()) do --destroy map
                if v.Name == "Map%d" then
                    v:Destroy()
                end
            end
            for i,v in pairs(a) do
                v.Character:WaitForChild("Humanoid").Health = 0
            end

        end --end roundend if statement
    end --end round begin   
end --end loop


1 comment

0

The problem is that it's not even starting from the beginning. It won't change the Gui like I wanted it to in the beginning.

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