Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I fix this code, because it seems to be broken?

Asked by
QWJKZXF 88
3 years ago

I have recently just searched up youtube and followed a tutorial on how to create a sword-fighting game, but after following their instructions, the game doesn't seem to work even after copying their code word for word, Can someone help me take a look at what's wrong with this code, or is this code broken?

-- 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 reward = 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 = {}  --plrs means players

    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)

    local AvailableMaps = MapsFolder:GetChildren()

    local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

    Status.Value = ChosenMap.Name.." Choosen"

    local ClonedMap = ChosenMap:Clone()
    ClonedMap.Parent = game.Workspace

    -- Teleport players to the map

    local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

    if not SpawnPoints then
        print("Spawnpoints not found!")
    end

    local AvailableSpawnPoints = SpawnPoints:GetChildren()

    for i, player in pairs(plrs) do
        if player then
            character = player.Character

            if character then
                -- Teleport them

                character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
                table.remove(AvailableSpawnPoints,1)

                --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


    Status. Value = "Get Ready..."

    wait(1)

    for i = GameLength,0,-1 do

        for x,player in pairs(plrs) do
            if player then
                character = player.Character

                if not character then
                    --Left the game
                else
                    if character:FindFirstChild("GamerTag") then
                        --They are still alive
                        print(player.Name.."is still alive!")
                    else
                        --They are dead
                        table.remove(plrs,x)
                        print(player.Name.."has been annihilated!")
                    end
                end
            else
                table.remove(plrs,x)
                print(player.Name.."has been annihilated!")
            end
        end

        Status.Value = "There are"..i.." seconds remaining, and "..#plrs" players are still alive"


        if #plrs == 1 then
            --Last person standing
            Status.Value = "The winner is"..plrs[1].Name
            plrs[1].leaderstats.Bucks.Value = plrs[1].leaderstats.Bucks.Value + reward
            break
        elseif #plrs == 0 then
            Status.Value = "No survivors!"
            break
        elseif i == 0 then
            Status.Value = "Times Up!"
            break
        end

        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

        player:LoadCharacter()
    end

    ClonedMap:Destroy()

    Status.Value = "Game Over!"

    wait(2)
end
0
I would be more inclined to help if you weren't asking me to revise 163 lines of code.. Maybe if you isolated a few errors that you're having that would get you help faster. Rather than trying to shove it all off onto someone else. Roger111 347 — 3y
0
Does it print any errors? ThatDevTim 188 — 3y
0
Found the error, line 114 you forgot your ".." after #plrs . Revised code ~~~~~~~~~~ ThatDevTim 188 — 3y
0
Found the error, line 114 you forgot your ".." after #plrs . Revised code: Status.Value = "There are"..i.." seconds remaining, and "..#plrs.." players are still alive" ThatDevTim 188 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Found the error, line 114 you forgot your ".." after #plrs . Revised code:


Status.Value = "There are"..i.." seconds remaining, and "..#plrs.." players are still alive"
Ad

Answer this question