2 local scripts inside player gui, to make a progress bar that displays others player position with a pfp (like tower of hell) i dont know if this affects the performance or how could i fix it if it does
local puttingFolder = script.Parent.Parent:WaitForChild("Players")
local thumbnailType = Enum.ThumbnailType.HeadShot
local thumbnailSize = Enum.ThumbnailSize.Size150x150
local players = game:GetService("Players")
local imageFrame = script.Parent:WaitForChild("Player")
local puttingFolder = script.Parent.Parent:WaitForChild("Players")
local checkpoints = #game.Workspace:WaitForChild("Checkpoints"):GetChildren()
for _, player in pairs(players:GetPlayers()) do
local imageToPut = imageFrame:Clone()
imageToPut.Image = players:GetUserThumbnailAsync(player.UserId, thumbnailType, thumbnailSize)
imageToPut.Name = player.UserId
imageToPut.Parent = puttingFolder
imageToPut.Position = UDim2.fromScale(player:WaitForChild("leaderstats"):WaitForChild("Stage").Value/checkpoints, .5)
end
players.PlayerAdded:Connect(function(player)
local imageToPut = imageFrame:Clone()
imageToPut.Image = players:GetUserThumbnailAsync(player.UserId, thumbnailType, thumbnailSize)
imageToPut.Name = player.UserId
imageToPut.Parent = puttingFolder
imageToPut.Position = UDim2.fromScale(player:WaitForChild("leaderstats"):WaitForChild("Stage").Value/checkpoints, .5)
end)
players.PlayerRemoving:Connect(function(player)
puttingFolder[player.UserId]:Destroy()
end)
local puttingFolder = script.Parent:WaitForChild("Players")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local checkpoints = #game.Workspace:WaitForChild("Checkpoints"):GetChildren()
local thumbnailType = Enum.ThumbnailType.HeadShot
local thumbnailSize = Enum.ThumbnailSize.Size150x150
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear)
while true do
task.wait()
for _, pfp in pairs(puttingFolder:GetChildren()) do
local player = players:GetPlayerByUserId(pfp.Name)
player:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
tweenService:Create(pfp, tweenInfo, {Position = UDim2.fromScale(player:WaitForChild("leaderstats"):WaitForChild("Stage").Value/checkpoints, .5)}):Play()
end)
end
end
The second script probably will affect performance, as you are creating connections within a while loop.
Instead of having a while true do for it, instead have just the for loop, then have a ChildAdded connection for the puttingFolder, which has the same internal code as the for loop.
Code
local puttingFolder = script.Parent:WaitForChild("Players")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local checkpoints = #game.Workspace:WaitForChild("Checkpoints"):GetChildren()
local thumbnailType = Enum.ThumbnailType.HeadShot
local thumbnailSize = Enum.ThumbnailSize.Size150x150
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear)
for _, pfp in pairs(puttingFolder:GetChildren()) do
local player = players:GetPlayerByUserId(pfp.Name)
player:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
tweenService:Create(pfp, tweenInfo, {Position = UDim2.fromScale(player:WaitForChild("leaderstats"):WaitForChild("Stage").Value/checkpoints, .5)}):Play()
end)
puttingFolder.ChildAdded:Connect(function(pfp)
local player = players:GetPlayerByUserId(pfp.Name)
player:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
tweenService:Create(pfp, tweenInfo, {Position = UDim2.fromScale(player:WaitForChild("leaderstats"):WaitForChild("Stage").Value/checkpoints, .5)}):Play()
end)
You may wish to move the actual logic to a function, and call the function instead, to avoid repetition of code.
It’s best to avoid creating connections within an infinite loop unless you’re very careful about disconnecting them, which you aren’t currently doing. If the reason as to why you did so was because the script is a LocalScript, and not all Stages are loaded in time, then this is how you’ll need to create the connections in-order to avoid the memory leak:
local puttingFolder = script.Parent:WaitForChild("Players")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local checkpoints = #game.Workspace:WaitForChild("Checkpoints"):GetChildren()
local thumbnailType = Enum.ThumbnailType.HeadShot
local thumbnailSize = Enum.ThumbnailSize.Size150x150
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)
local function onChildAdded(pfp)
local player = players:GetPlayerByUserId(pfp.Name)
if player then -- GetPlayerByUserId can return nil if no player is found
local leaderstats = player:WaitForChild("leaderstats")
local stage = leaderstats:WaitForChild("Stage")
stage.Changed:Connect(function(value) -- Using Changed event is more convenient for value Instances
tweenService:Create(pfp, tweenInfo, {Position = UDim2.fromScale(value / checkpoints, 0.5)}):Play()
end)
end
end
puttingFolder.ChildAdded:Connect(onChildAdded)
for _, pfp in puttingFolder:GetChildren() do onChildAdded(pfp) end
The first script seems to be ok from what I can tell