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

Script is running faster than startergui can put gui inside player?

Asked by
Azuc 112
6 years ago

So I keep getting an error telling me "Main" is not part of the playergui. I added a line of code to the main script to tell it to wait until it is but it still gives me that error each time. Its like the script is running faster than the startergui can put the gui "Main" in the player

local PlayerBase = game:GetService("Players")
        PlayerBase.PlayerAdded:connect(function(PlayerUI)
    repeat wait() until PlayerUI.PlayerGui:WaitForChild("Main")
end)


--// Tables

winners = {}

--// Modules

Settings = require (script.Settings)

--// Game Variables

Intermission = Settings.Intermission
GameTime = Settings.GameTime

--// Functions

function ChangeText(Text)
    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        Players[i].PlayerGui.Main.Time.Text = Text
    end
end

function Winners(Text)
    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        Players[i].PlayerGui.Main.YouWin.Text = Text
    end
end

function GetMap()
    local Maps = game.ReplicatedStorage.Maps:GetChildren()
    local PickMap = math.random(1, #Maps)
    local SelectedMap = Maps[PickMap]:Clone()
    SelectedMap.Name = 'Map'
    SelectedMap.Parent = game.Workspace
end

function RemoveMap()
    if game.Workspace:FindFirstChild('Map') then
       game.Workspace.Map:Remove()
    end
end

function Alive()
    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        Players[i].Alive.Value = true
    end
end

--// Main Script


game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        hum.Died:Connect(function()
            if game.Workspace.GameActive.Value == true and player.Playing.Value == true then
            player.PlayerGui.Main.YouLose.Text = ('Darn! Try Again!')
            player.Alive.Value = false
            player.Playing.Value = false
            wait(1.5)
            player.PlayerGui.Main.YouLose.Text = ('')
            else
            player.Alive.Value = false
            player.Playing.Value = false
            end
        end)
    end)
end)

while true do

    for i = Intermission, 0, -1 do
        ChangeText('Intermission: ' ..i)
        wait(1)
    end

    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        Players[i].Playing.Value = true
    end

    Alive()
    wait()

    GetMap()
    wait()
    ChangeText('Loading Map')
    wait(1)
    ChangeText('Loading Map.')
    wait(1)
    ChangeText('Loading Map..')
    wait(1)
    ChangeText('Loading Map...')
    wait(2)
    ChangeText('Loaded')
    wait()

    game.Workspace.GameActive.Value = true

    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        if Players[i].Playing.Value == true and Players[i].Alive.Value == true then
            local Spawn = game.Workspace.Map.Spawns.Spawn
            Players[i].Character.HumanoidRootPart.CFrame = CFrame.new(Spawn.Position)
            Spawn:remove()
            Players[i].Character.Humanoid.WalkSpeed = 0
        end
    end

    for i = 5, 0, -1 do
    ChangeText('Game Starting In: ' ..i)
    wait(1)
    end

    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        if Players[i].Playing.Value == true then
            Players[i].Character.Humanoid.WalkSpeed = 16
        end
    end

    ChangeText('Begin!')
    game.Workspace.AllowEntry.Value = true
    wait(1)

    for i = GameTime, 0, -1 do
        ChangeText('Time Remaining: ' ..i)
        wait(1)
        ChangeText('GAME OVER')
    end


    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        if Players[i].Playing.Value == true and Players[i].Alive.Value == true then
           table.insert(winners, 1, Players[i].Name)
           Players[i].leaderstats.Coins.Value = Players[i].leaderstats.Coins.Value + 5
           Players[i].leaderstats.Wins.Value = Players[i].leaderstats.Wins.Value + 1
           Players[i].Character:BreakJoints()
           Players[i].Playing.Value = false
           Players[i].PlayerGui.Main.YouWin.Text = ('You Win! +5 Coins!')
           wait(5)
        end
    end

    RemoveMap()
    game.Workspace.GameActive.Value = false
    game.Workspace.AllowEntry.Value = false
    for i,v in pairs (winners) do print(v) 
    end
    wait(4)

end

0
Try putting the repeat at the very top so none of the code will even run without it. SimpleFlame 255 — 6y
0
(Assuming your game is FE) If this is in a script then the client's PlayerGui is empty from the server LaeMVP 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

I see the problem, when you are calling the PlayerAdded event, Lua will execute the code inside of that event and nothing else. Lets say you want to add a WaitForChild() code in the PlayerAdded event (which you did lol) it will only wait for the child and move on, not passing it on to the that script. So I would advice you to change every .Main to :WaitForChild("Main").

Example;

Player.PlayerGui:WaitForChild("Main")
--or--
Player[i].PlayerGui:WaitForChild("Main") --the [i] depends if you are using a for loop

Hope this helped!

TIP: I would advise to hold Ctrl + H (for Windows, but for Mac; Command + H), a frame will appear on the top right hand corner of your screen and you will tell why I said that ;)!

Ad

Answer this question