Hello!
I have a custom player list, and the custom player list will display a player's: Avatar, Name, Round Kills, and Killstreak. Only problem is, it doesn't update the Round Kills/Killstreak for the correct user in real time.
Example: Player 1 has 2 RoundKills and a Killstreak of 2. On Player 1's Display, it shows these stats belonging to Player 2 instead of the actual player. How do I fix this? I know I need it to check for all the players, but I'm not sure how to do that correctly, my prior attempts have failed.
In my Leaderstats script I have this bit of code:
local updatedPlayerKillstreak = game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate") game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate"):FireClient(Player, updatedPlayerKillstreak, Killstreak.Value) print("Killstreak check") local updatedPlayerKills = game:GetService("ReplicatedStorage"):WaitForChild("KillUpdate") game:GetService("ReplicatedStorage"):WaitForChild("KillUpdate"):FireClient(Player, updatedPlayerKills, Roundkills.Value) print("RoundKills check")
And under StarterGui I have my ScreenGui named "PlayerList" and a Local Script under that named "Main", this is the script below:
game.StarterGui:SetCoreGuiEnabled("PlayerList", false) local plr = game.Players.LocalPlayer local kills = plr.leaderstats.Roundkills.Value local killstreak = plr.leaderstats.Killstreak.Value local Avatarurl = "http://www.roblox.com/Thumbs/Avatar.ashx?x=500&y=500&Format=Png&username=" local gui = script.Parent while wait(1) do for _,plr in pairs (game.Players:GetChildren()) do if script.Parent.Holder:FindFirstChild(plr.Name) then else local kills2 = plr.leaderstats.Roundkills.Value local killstreak2 = plr.leaderstats.Killstreak.Value I = Instance.new("Frame") I.Name = plr.Name I.Parent = script.Parent.Holder I.Style = "DropShadow" -- NAME T = Instance.new("TextLabel") T.Name = "Username" T.BackgroundTransparency = 1 T.Text = plr.Name T.Parent = I T.Size = UDim2.new(0, 165, 0, 35) T.Position = UDim2.new(0, 10, 0, 0) if plr.Name == game.Players.LocalPlayer.Name then T.TextColor3 = Color3.fromRGB(0, 200, 255) else T.TextColor3 = Color3.new(1, 1, 1) end T.TextStrokeColor3 = Color3.new(0, 0, 0) T.TextStrokeTransparency = 0.5 T.TextXAlignment = "Left" T.Font = "SourceSansBold" T.TextSize = 20 -- KILLS K = Instance.new("TextLabel") K.Name = "RoundKills" K.BackgroundTransparency = 1 K.Text = kills2 K.Parent = I K.Size = UDim2.new(0, 20, 0, 35) K.Position = UDim2.new(0, 155, 0, 0) K.TextColor3 = Color3.new(1, 1, 1) K.TextStrokeColor3 = Color3.new(0, 0, 0) K.TextStrokeTransparency = 0.5 K.TextXAlignment = "Right" K.Font = "SourceSansBold" K.TextSize = 20 -- KILL STREAK KS = Instance.new("TextLabel") KS.Name = "KillStreak" KS.BackgroundTransparency = 1 KS.Text = killstreak2 KS.Parent = I KS.Size = UDim2.new(0, 20, 0, 35) KS.Position = UDim2.new(0, 167, 0, 0) KS.TextColor3 = Color3.new(255,0,0) KS.TextStrokeColor3 = Color3.new(0, 0, 0) KS.TextStrokeTransparency = 0.5 KS.TextXAlignment = "Right" KS.Font = "SourceSansBold" KS.TextSize = 20 -- AVATAR IMAGE P = Instance.new("ImageLabel") P.ScaleType = "Slice" P.Name = "AvatarImage" P.BackgroundTransparency = 1 P.Image = Avatarurl..plr.Name P.Parent = I P.Size = UDim2.new(.25,0,1.25,0) P.Position = UDim2.new(-0.25, 0, 0, 0) game:GetService("ReplicatedStorage"):WaitForChild("KillUpdate").OnClientEvent:Connect(function(updatedPlayerKills, value) if(updatedPlayerKills.Name == plr.Name) then K.Text = value end end) game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate").OnClientEvent:Connect(function(updatedPlayerKillstreak, value) if(updatedPlayerKillstreak.Name == plr.Name) then KS.Text = value end end) end end end
first off, you do this every second? making a whole new unupdated gui (using values read at initialization, such as killstreak, then waiting for the event to update it? I may misunderstand, I read it quickly.
Without the while loop, it's decent but it doesn't know which player to update: The event sends only a new value, but not the player to whom it applies. You should have it send a second argument, indicating which stat to update in the gui:
game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate").OnClientEvent:Connect(function(updatedPlayer, value) if(updatedPlayer.Name == plr.Name) then KS.Text = value end end)
And of course the same for the killupdate, and also update the remote event code:
local updatedPlayer = <a way to know which player's killstreak was changed> game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate"):FireClient(Player, updatedPlayer, Killstreak.Value)
Edit: added code block
Edit 2: The changed code you asked:
--event script: game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate"):FireAllClients(Player.Name, Killstreak.Value)
--localscript: game:GetService("ReplicatedStorage"):WaitForChild("KillStreakUpdate").OnClientEvent:Connect(function(updatedPlayerName, value) if(updatedPlayerName == plr.Name) then --<removed the dot! KS.Text = value end end)