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

Update Global GUI Board From Values Inside ServerStorage?

Asked by 6 years ago

I have a playeradded script that adds a player as a folder and includes stats for the game. How do I have it so that if a player joins, the stats board will be updated depending on what has happened during the game.

Data Store Script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("TA5970AJ3B")

local ServStorage = game:GetService("ServerStorage")
local Players = ServStorage:WaitForChild("Players")

local RepStorage = game:GetService("ReplicatedStorage")
local StatsChanged = RepStorage:WaitForChild("StatsChanged")
local PointsEvent = StatsChanged:WaitForChild("Points")
local ReboundsEvent = StatsChanged:WaitForChild("Rebounds")
local StealsEvent = StatsChanged:WaitForChild("Steals")
local BlocksEvent = StatsChanged:WaitForChild("Blocks")
local AssistsEvent = StatsChanged:WaitForChild("Assists")

local UpdateBoard = RepStorage:WaitForChild("StatUpdate")

game.Players.PlayerAdded:Connect(function(player)
    --Variables--
    local Namee = Instance.new("Folder", Players) --their own folder
    Namee.Name = player.Name    

    local folder = Instance.new("Folder", Namee) --inside their folder, a stats folder
    folder.Name = 'Stats'

    local pts = Instance.new("IntValue", folder)
    pts.Name = 'Points'
    pts.Value = 0

    local ast = Instance.new("IntValue", folder)
    ast.Name = 'Assists'
    ast.Value = 0

    local reb = Instance.new("IntValue", folder)
    reb.Name = 'Rebounds'
    reb.Value = 0

    local steal = Instance.new("IntValue", folder)
    steal.Name = 'Steals'
    steal.Value = 0

    local block = Instance.new("IntValue", folder)
    block.Name = 'Blocks'
    block.Value = 0

    pts.Changed:Connect(function()
        PointsEvent:FireAllClients(player, pts.Value)
    end)

    ast.Changed:Connect(function()
        AssistsEvent:FireAllClients(player, ast.Value)
    end)

    reb.Changed:Connect(function()
        ReboundsEvent:FireAllClients(player, reb.Value)
    end)

    steal.Changed:Connect(function()
        StealsEvent:FireAllClients(player, steal.Value)
    end)

    block.Changed:Connect(function()
        BlocksEvent:FireAllClients(player, block.Value)
    end)

    player.CharacterAdded:Connect(function(character) --update board for when a player joins
        UpdateBoard:FireClient(player, 
            pts.Value, ast.Value, 
            reb.Value, steal.Value,
            block.Value)
    end)

end)

game.Players.PlayerRemoving:Connect(function(player)
    for i, v in pairs(Players:GetChildren()) do
        if v.Name == player.Name then
            v:Destroy()
        end
    end
end)

Local Script:

local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")

local Stats = playergui:WaitForChild("Stats")
local StatFrame = Stats:WaitForChild("Frame")
local PlayerFolder = StatFrame:WaitForChild("PlayerFolder")

local RepStorage = game:GetService("ReplicatedStorage")
local UpdateBoard = RepStorage:WaitForChild("StatUpdate")

UpdateBoard.OnClientEvent:Connect(function(pts, ast, reb, steal, block)
    for i, v in pairs(game.Players:GetPlayers()) do --trying to define each player
        local Players = PlayerFolder:GetChildren()
        local PlayersFrame = Players:WaitForChild(v.Name)
        local StatsFrame = PlayersFrame:WaitForChild("Stats")
        local pointstext = StatsFrame:WaitForChild("Points")
        local asttext = StatsFrame:WaitForChild("Assists")
        local rebtext = StatsFrame:WaitForChild("Rebounds")
        local stealtext = StatsFrame:WaitForChild("Steals")
        local blocktext = StatsFrame:WaitForChild("Blocks")
        pointstext.Text = pts.Value
        asttext.Text = ast.Value
        rebtext.Text = reb.Value
        stealtext.Text = steal.Value
        blocktext.Text = block.Value
    end
end)

Answer this question