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

Game loop for sword fight game, will not teleport players or start script shown. help?

Asked by 4 years ago
Edited by SerpentineKing 4 years ago

The script does not teleport players, start the game loop. No errors are shown when executed, but the game,( as I said) will not start. If there are errors that I am unaware of that would stop the script from working, please tell me. The game loop is supposed to teleport, Put players in a table and monitor the game.

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")


local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 120

local reward = 1

-- Game Loop
while true do
Status.Value = "Waiting for more Players..."
repeat wait(1) until game.Players.NumPlayers > 1

for i = 15, 0, -1 do
        local fulltext = "("..i..")" -- change to the text you want
        local text = "Intermission"

        for i = 1,#fulltext do
         text = fulltext:sub(1,i)
        Status.Value = ""
         wait()
        end
    wait(0.95)
    end


local plrs = {}

for i, player in pairs(game.Players:GetPlayers()) do
    if player then
        table.insert(plrs, player) -- Add each player into players table
    end
end

wait(1)

local AvailableMaps = MapsFolder:GetChildren()

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

local fulltext = ChosenMap.Name.."" -- change to the text you want
local text = ""

for i = 1,#fulltext do
 text = fulltext:sub(1,i)
 Status.Value = ""
 wait()
end

wait(5)

for i = 10, 0, -1 do
        local fulltext = "Players will be teleported in ("..i..")" -- change to the text you want
        local text = ""

        for i = 1,#fulltext do
         text = fulltext:sub(1,i)
        Status.Value = 
         wait()
        end
    wait(0.4)
    end

local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace



wait(1)

--Teleport players to the map

local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

if not SpawnPoints then
    print("Spawn points not found!")        
end

local AvailableSpawnPoints = SpawnPoints:GetChildren()

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


    character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
    table.remove(AvailableSpawnPoints,1)

    local Sword = ServerStorage.Sword:Clone()
    Sword.Parent = player.Backpack

    local GameTag = Instance.new("BoolValue")
    GameTag.Name = "GameTag"
    GameTag.Parent = player.Character
    else
     if not player then
                    table.remove(plrs,i)
                    end
               end
    end

end

if ClonedMap:FindFirstChild("SpawnPoints") then
    ClonedMap.SpawnPoints:Destroy()
end

local fulltext = "Get Ready to Play!" -- change to the text you want
local text = ""

for i = 1,#fulltext do
 text = fulltext:sub(1,i)
 Status.Value = text
 wait()
end


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
                table.remove(plrs,x)
            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)
                    print(player.Name.." has been removed!")
                end
            end
        else
            table.remove(plrs,x)
            print(player.Name.." has been removed!")
        end
    end

local fulltext = ""..i.." seconds remaining" -- change to the text you want
local text = ""

for i = 1,#fulltext do
 text = fulltext:sub(1,i)
 Status.Value = text
 wait()
end

    if #plrs == 1 then
        -- Last person standing"The winner is "..plrs[1].Name
        local fulltext = "The winner is "..plrs[1].Name -- change to the text you want
        local text = ""

        for i = 1,#fulltext do
        text = fulltext:sub(1,i)
        Status.Value = text
        wait()
        end
        plrs[1].leaderstats.Wins.Value = plrs[1].leaderstats.Wins.Value + reward
        wait(5)
        break
    elseif #plrs == 0 then
         local fulltext = "Nobody won!" -- change to the text you want
        local text = ""

        for i = 1,#fulltext do
        text = fulltext:sub(1,i)
        Status.Value = text
        wait()
        end
        wait(3)
        break
    elseif i == 0 then
         local fulltext = "Times up!" -- change to the text you want
        local text = ""

        for i = 1,#fulltext do
        text = fulltext:sub(1,i)
        Status.Value = text
        wait()
        end
        wait(3)
        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()

        if player.Backpack:FindFirstChild("Sword") then
            player.Backpack.Sword:Destroy()
        end

        if character:FindFirstChild("Sword") then
            character.Sword:Destroy()
end
end

ClonedMap:Destroy()

player:LoadCharacter()

local fulltext = "Game Ended" -- change to the text you want
        local text = ""

        for i = 1,#fulltext do
        text = fulltext:sub(1,i)
        Status.Value = text
        wait()
        end
end
end
end

Local Script

local Status = game:GetService("ReplicatedStorage"):WaitForChild("Status") -- this will be used as a variable

    script.Parent.Text = Status.Value -- this tells when it changes

    Status:GetPropertyChangedSignal("Value"):Connect(function() -- and when it changes again this will be used
    script.Parent.Text = Status.Value

    end)

there you go

1
On line 088 - Upercase the "c" in player.character! its supposed to be player Character. Try it and see if there are any changes! Tizzel40 243 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Improvements

Use a local function instead of repeating code for the fulltext / text piece

string.sub() should be used to get parts of a text, not :sub()

Use :GetService() for the Players and Workspace services

Use :SetPrimaryPartCFrame() to move the player Character model instead of just the HumanoidRootPart

Issues

The length of a string is found via string.len() not # which is used for tables

Lua is case sensitive, Line 88, character should be capitalized

In some sections of your code, Status.Value is left as "" instead of updated with the text and Line 63 the line is incomplete

Use #(Players:GetPlayers()) to get the number of players in the game

Revised Server Script

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

local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 120
local reward = 1

local function PrintType(text)
    if string.len(text) > 0 then
        for i = 1, string.len(text), 1 do
            Status.Value = string.sub(text, 1, i)
            wait(0.05)
        end
    end
end

-- Game Loop
while true do
    -- Wait To Begin
    Status.Value = "Waiting for more Players..."
    repeat
        wait(1)
    until #(Players:GetPlayers()) > 1

    -- Intermission
    PrintType("Intermission")
    for i = 15, 0, -1 do
        Status.Value = ("Intermission ("..i..")")
        wait(0.95)
    end

    -- Players in Round
    local plrs = {}
    for i, player in pairs(Players:GetPlayers()) do
        if player then
            table.insert(plrs, (#plrs + 1), player)
        end
    end

    -- Choose Map
    wait(1)
    local AvailableMaps = MapsFolder:GetChildren()
    local ChosenMap = AvailableMaps[math.random(1, #AvailableMaps)]

    PrintType(ChosenMap.Name)
    wait(5)
    PrintType("Players will be teleported in")

    for i = 10, 0, -1 do
        Status.Value = ("Players will be teleported in ("..i..")")
        wait(0.4)
    end

    local ClonedMap = ChosenMap:Clone()
    ClonedMap.Parent = game:GetService("Workspace")
    wait(1)

    -- Teleport Players
    local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
    if not SpawnPoints then
        print("Spawn points not found!")
    else
        local AvailableSpawnPoints = SpawnPoints:GetChildren()
        for j, player in pairs(plrs) do
            if player then
                character = player.Character
                if character then
                    character:SetPrimaryPartCFrame(AvailableSpawnPoints[j].CFrame + Vector3.new(0, 10, 0))

                    local Sword = ServerStorage:WaitForChild("Sword"):Clone()
                    Sword.Parent = player.Backpack

                    local GameTag = Instance.new("BoolValue")
                    GameTag.Name = "GameTag"
                    GameTag.Parent = player.Character
                else
                    if not player then
                        table.remove(plrs, j)
                    end
                end
            end
        end

        if ClonedMap:FindFirstChild("SpawnPoints") then
            ClonedMap.SpawnPoints:Destroy()
        end

        PrintType("Get Ready to Play!") 
        wait(1)

        -- Keep Track of Players during Round
        for j = GameLength, 0, -1 do
            for x, player in pairs(plrs) do
                if player then
                    character = player.Character
                    if not character then
                        -- Left Game
                        table.remove(plrs, x)
                    else
                        if character:FindFirstChild("GameTag") then
                            -- Alive
                            print(player.Name.." is still in the game!")
                        else
                            -- Died
                            table.remove(plrs, x)
                            print(player.Name.." has been removed!")
                        end
                    end
                else
                    table.remove(plrs, x)
                    print(player.Name.." has been removed!")
                end
            end

            Status.Value = (j.."seconds remaining")

            if #plrs == 1 then
                -- Last Person Standing
                PrintType("The winner is "..plrs[1].Name)

                local Wins = plrs[1]:WaitForChild("leaderstats"):WaitForChild("Wins")
                Wins.Value = Wins.Value + reward
                wait(5)
                break
            elseif #plrs == 0 then
                PrintType("Nobody won!")
                wait(3)
                break
            elseif j == 0 then
                PrintType("Times up!")
                wait(3)
                break
            end
            wait(1)
        end
    end

    -- Respawn Players, Round Ended
    print("End of game")
    for i, player in pairs(Players:GetPlayers()) do
        character = player.Character
        if not character then
            -- Ignore Them
        else
            if character:FindFirstChild("GameTag") then
                character.GameTag:Destroy()

                if player:WaitForChild("Backpack"):FindFirstChild("Sword") then
                    player:WaitForChild("Backpack").Sword:Destroy()
                end

                if character:FindFirstChild("Sword") then
                    character.Sword:Destroy()
                end
            end

            ClonedMap:Destroy()
            player:LoadCharacter()
        end
    end
    PrintType("Game Ended")
end

Revised Local Script

local Status = game:GetService("ReplicatedStorage"):WaitForChild("Status")
local Label = script.Parent
Label.Text = Status.Value

Status:GetPropertyChangedSignal("Value"):Connect(function()
    Label.Text = Status.Value
end)
Ad
Log in to vote
0
Answered by 4 years ago

So since you said you didnt have a script inside of the GUI/Status here

local Status = game:GetService("ReplicatedStorage"):WaitForChild("Status") -- this will be used as a variable

script.Parent.Text = Status.Value -- this tells when it changes

Status:GetPropertyChangedSignal("Value"):Connect(function() -- and when it changes again this will be used

script.Parent.Text = Status.Value

end)

Answer this question