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

Use datastore to update GUI for all players?

Asked by 4 years ago

I am coding my fist a game. The game works -- sort of--, although I know the code is not great. It's a game for small children in which a number shows on a sign and the player has to go to the platform that matches the number. One of the issues that I have is that I want there to be a GUI that displays the points in big numbers as the leader stats on the top corner are not easy to see. What I want is not just for the player to see their own points this would be somewhat easier, just add a textLabel on the starter player gui with a script like so:

while true do 
    wait(.1)
    script.Parent.Text = game.Players.LocalPlayer.leaderstats.Platforms.Value
end
with the following code too:

game.Players.PlayerAdded:connect(function(player)
    local playerKey = "Player_" .. player.UserId
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"
    local Platforms = Instance.new("IntValue", leaderstats) 
    Platforms.Name = "Platforms"
    Platforms.Value = 0
    platformCount:SetAsync(player.UserId, 0)
    StartGui:FireAllClients(player)

end)

Please take note of the last couple of lines of that block, those are what I came up with. I am not sure how to do it better but I sense they are a botch.

My objective is that each player can see not only their scores updated in real time, but also every other player's. This is what is not fully working. My method is as follows. When a player steps on the desired platform I have this code on a local script

                allPlats[k].Eliminated = true
                player:WaitForChild("leaderstats"):FindFirstChild("Platforms").Value = player:WaitForChild("leaderstats"):FindFirstChild("Platforms").Value + 1 --increase leaderstats
                points = points + 1
                numIfScored:InvokeServer(column, row, teamColorReady, points, team)
                PlatformDeleted.OnClientEvent:Connect(platformTransparency)

Ignore the lines that don't apply to the question. The important part for this question is that I access the intValue created in the leaderstats and increase it's value by one. I also send a call to the server with a remote function that passes where the platform was, the color of the team etc...

The serverside script then uses these values to perform a number of tasks related to setting the next target and updating the way the platforms look and, crucially, sends the data to a datastore:

function numIfScored.OnServerInvoke(plr, col, row, orignalTeamColor, points, team)

game.Workspace.TargetNumber.Value = getAnddisplayTarget(col, row)

PlatformDeleted:FireAllClients(col, row)

for k, v in pairs(allPlats) do 
    if v.Column == col and v.Row == row then
        v.Part.Transparency = 0
        v.Part.Material = "Neon"
        v.Part.BrickColor = BrickColor.new(orignalTeamColor)
    end
end
platformCount:SetAsync(plr.UserId, points)
end

I then run a separate block of code in the same scrip that is constantly updating the gui by grabbing the points from the datastore:

while wait(.1) do   
    for _, player in pairs(Players:GetPlayers()) do

        local points = platformCount:GetAsync(player.UserId)
        UpdateGUI:FireAllClients(player.UserId,points, player.Team)
    end
end

I have a localscript that is a child of the text label that displays the scores with the following code:

local function UpdateLabel(plrId, points, team)

    if team.Name == "Really red Team" then
    game.Players.LocalPlayer.PlayerGui.ScreenGui.RedTeam.Text = points
    elseif team.Name == "Really blue Team" then
    game.Players.LocalPlayer.PlayerGui.ScreenGui.BlueTeam.Text = points
    end
end
StartGui.OnClientEvent:Connect(StartLabel)

This works but is buggy. I play at home with my son an it sort of works, with a bit of lag, but as soon as I try to play with other players, like my friend and his son, the system stops working well. One thing that I know is not good is that if you start playing before another player joins, then that player can't see your progress before they came in. Another problem is that the first player that joins will be seen by players that join later, but as players join that can't see the labels of players that joined before.

Is there a much simpler way to create a gui that updates all the players scores in all the players gui's? Thanks.

I can post full code if needed.

Answer this question