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!
(Two people playing through a local server):
You can tell one of their GUI’s isn’t updating.