My script is not working!

Hello, I am a beginner scripter and I decided to watch AlvinBlox’s game tutorial for a sword fighting game. Unfortunately my script does not work as not only do the players spawn in the map, they also don’t get their swords, unfortunately the Gui for the intermission does not work and nor does the game ever end and the loop restarts… I need help on fixing it. Here is my script:

-- Define Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 50

local winreward = 25

-- Game Loop

while true do 
	
	Status.Value = "Waiting for enough players"
	
	repeat wait(1) until game.Players.NumPlayers >=2
	
	Status.Value = "Intermission"
	
	wait(10)
	
	local plrs = {}
	
	print("Start of game loop before first for loop worked")
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into plrs table
		end
	end
	wait(2)
	
	print("End of for loop worked")
	
	local AvailableMaps = MapsFolder:GetChildren()
	
	local ChosenMap = AvailableMaps[math.random(1, #AvailableMaps)]
	
	Status.Value = ChosenMap.Name.."Chosen"
	
	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace
	
	print("All code before teleportation worked!")
	
	-- Teleport players to the map
	
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	
	if not SpawnPoints then
		print("Spawnpoints not found!")
	end
	
	print("First if statement of teleportation worked!!!")
	
	local AvailableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs (plrs) do
		if player then
			character = player.Character
			
			print("For loop in teleportation worked!")
			
			if character then
				-- Teleport Them
				
				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
				table.remove(AvailableSpawnPoints, 1)
				
				print("Teleporting players worked")
				
				-- Give them a sword
				
				local sword = ServerStorage.Sword:Clone()
				sword.Parent = player.BackPack
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "Gametag"
				GameTag.Parent = player.Character
			else
				-- There is no character
				if not player then
					table.remove(plrs, i)
				end
			end
		end
	end
	
	print("Giving the sword worked")
	
	Status.Value = "Get ready to play!"
	
	wait(2)
	
	for i = GameLength,0,-1 do
		
		for x, player in pairs(plrs) do
			if player then
				
				character = player.Character
				
				if not character then
					-- Left game
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						print(player.Name.."Is still in the game!")
					else
						-- They are dead
						table.remove(plrs, x)
					end
				end
				print("Checking if they are still in the game")
			else
				table.remove(plrs, x)
				print(player.Name.."Has been removed!")
			end
		end
		
		Status.Value = "There are "..i.." second remaining, and "..#plrs.." players left"
		
		print("Showing the amount of players left and stuff")
		
		if #plrs == 1 then
			-- Last person standing
			Status.Value = "The winner is "..plrs[1].Name
			plrs[1].leaderstats.Bucks.Vakye = plrs[1].leaderstats.Bucks.Value + winreward
			break
		elseif #plrs == 0  then
			Status.Value = "Nobody won!"
			break
		elseif i == 0 then
			Status.Value = "Time's up!"
			break
		end
		
		print("Checking if they won")
		
		wait(1)
	end
	
	print("End of game")
	
	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character
		
		if not character then
			-- Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			
			end
			
			if player.BackPack:FindFirstChild("Sword") then
				player.BackPack.Sword:Destroy()
			end
			
			if character:FindFirstCHild("Sword") then
				character.Sword:Destroy()
			end
			
		end
		
		print("Destroying all the parts after the game has ended")
		
		player:LoadCharacter()
	end
	
	ClonedMap:Destroy()
	
	Status.Value = "The game has ended"
	
	wait(2)
end

print("Final script test")

As you can see, some ways I tried fixing the problem was by adding print statements throughout the code. Here were my results…:

If you know a way to fix the problem please reply back on the devforum or message me through disc: Sep#2698

Thank you for reading! :grinning:

(Two people playing through a local server):

You can tell one of their GUI’s isn’t updating.

Tried to fix your problem, + you arent aviable in disc

-- Define Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 50

local winreward = 25

-- Game Loop

while true do 
	
	Status.Value = "Waiting for enough players"
	
	repeat wait(1) until #game.Players:GetPlayers() >=2
	
	Status.Value = "Intermission"
	
	wait(10)
	
	local plrs = {}
	
	print("Start of game loop before first for loop worked")
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into plrs table
		end
	end
	wait(2)
	
	print("End of for loop worked")
	
	local AvailableMaps = MapsFolder:GetChildren()
	
	local ChosenMap = AvailableMaps[math.random(1, #AvailableMaps)]
	
	Status.Value = ChosenMap.Name.." Chosen"
	
	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace
	
	print("All code before teleportation worked!")
	
	-- Teleport players to the map
	
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	
	if not SpawnPoints then
		print("Spawnpoints not found!")
                return
	end
	
	print("First if statement of teleportation worked!!!")
	
	local AvailableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs (plrs) do
		if player then
			character = player.Character
			
			print("For loop in teleportation worked!")
			
			if character then
				-- Teleport Them
				
                                local randPoint = AvailableSpawnPoints[math.random(1,#AvailableSpawnPoints)

				character:MoveTo(AvailableSpawnPoints[randPoint].Position)
				table.remove(AvailableSpawnPoints, randPoint)
				
				print("Teleporting players worked")
				
				-- Give them a sword
				
				local sword = ServerStorage.Sword:Clone()
				sword.Parent = player.Backpack
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "Gametag"
				GameTag.Parent = player.Character
			else
				-- There is no character
				if not player then
					table.remove(plrs, i)
				end
			end
		end
	end
	
	print("Giving the sword worked")
	
	Status.Value = "Get ready to play!"
	
	wait(2)
	
	for i = GameLength,0,-1 do
		
		for x, player in pairs(plrs) do
			if player then
				
				character = player.Character
				
				if not character then
					-- Left game
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						print(player.Name.."Is still in the game!")
					else
						-- They are dead
						table.remove(plrs, x)
					end
				end
				print("Checking if they are still in the game")
			else
				table.remove(plrs, x)
				print(player.Name.."Has been removed!")
			end
		end
		
		Status.Value = "There are "..i.." second remaining, and "..#plrs.." players left"
		
		print("Showing the amount of players left and stuff")
		
		if #plrs == 1 then
			-- Last person standing
			Status.Value = "The winner is "..plrs[1].Name
			plrs[1].leaderstats.Bucks.Vakye = plrs[1].leaderstats.Bucks.Value + winreward
			break
		elseif #plrs == 0  then
			Status.Value = "Nobody won!"
			break
		elseif i == 0 then
			Status.Value = "Time's up!"
			break
		end
		
		print("Checking if they won")
		
		wait(1)
	end
	
	print("End of game")
	
	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character
		
		if not character then
			-- Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			
			end
			
			if player.Backpack:FindFirstChild("Sword") then
				player.Backpack.Sword:Destroy()
			end
			
			if character:FindFirstCHild("Sword") then
				character.Sword:Destroy()
			end
			
		end
		
		print("Destroying all the parts after the game has ended")
		
		player:LoadCharacter()
	end
	
	ClonedMap:Destroy()
	
	Status.Value = "The game has ended"
	
	wait(2)
end

print("Final script test") -- it will never run, because its below endless loop

Hello thank you for replying, I will check if this script works tomorrow!

Hello, sorry I took so long to test it out but I finally got to test the script when an error popped up for line 74. When I ran the script it showed this:

SCRIPTING HELP SWORD FIGHTING #4

here is a screenshot of that part of the script:

SCRIPTING HELP SWORD FIGHTING #5

Thank you for reading!

I think the error may have been because you didn’t close the square bracket in line 72 when you were making the randPoint variable.

Its should be index or key.
In arrays we using indexes to get value

For example

local t = {"apple",6,true,math.pi}
print(t[2]) -- 6
print(t[4]) -- 3.14...

As I said before after this character (the line under this code) got an error. So I just added the square bracket at the end of this line and the error stopped.

Also, for the whole code saw that the while true do loop as no end statement… Should I put the end statement at the end of the code?

This will be a problem later on plrs[1].leaderstats.Bucks.Vakye = plrs[1].leaderstats.Bucks.Value + winreward. Try plrs[1].leaderstats.Bucks.Value += winreward