What do I want to achieve: Faster loading time of Scripts
What is the issue? Part of script will not load and glitches a bit without a task.wait(10). The part of the script that won’t load in time is the one where it displays the level and xP value from the player data.
What solutions have you tried so far? I have tried using WaitForChild() already.
—local Manager,addxP = nil
local maxWidth = 0.942
local Players = game:GetService("Players")
local Manager = require(game:GetService("ServerScriptService").PlayerData.Manager)
local addxP = require(game.StarterPlayer.StarterPlayerScripts.addxP)
local TweenService = game:GetService("TweenService")
Players.PlayerAdded:Connect(function(player)
task.wait(10)
print("playerdetected")
local bar = player.PlayerGui:WaitForChild("Level"):WaitForChild("Frame"):WaitForChild("Frame"):WaitForChild("Roundify")
local xP = player.PlayerGui:WaitForChild("Level"):WaitForChild("Frame"):WaitForChild("xP")
local Level = player.PlayerGui:WaitForChild("Level"):WaitForChild("Frame"):WaitForChild("Level")
local profile = Manager.Profiles[player]
if not profile then return end
while true do
local goal = {}
goal.Size = UDim2.new((profile.Data.xP/addxP.EF(profile.Data.Level)* maxWidth) , 8, 0, 8)
local tweenInfo = TweenInfo.new(3)
local tween = TweenService:Create(bar, tweenInfo, goal)
xP.Text = profile.Data.xP.."/"..addxP.EF(profile.Data.Level).." xP"
Level.Text = "Level: "..profile.Data.Level
tween:Play()
--bar.Size = UDim2.new((profile.Data.xP/addxP.EF(profile.Data.Level)* maxWidth) , 8, 0, 8)
task.wait()
end
end)
Thank you for your thoughts. The ui actually does load. The problem is the Level and xP do not load and when I do not put in that wait. What do you think?
The UI is not loading without the wait because it is not loaded in before the playeradded event is fired, can you try player.CharacterAdded event to see if that helps?
A better method is to have an integer variable for each level and xp, and change the variable values depending on the data. Then have all the ui work like text and tweening client-sided.
That’s what I was thinking of doing. I actually already have values are actually already changed depending on data so it could work lol. Thank you. Do you believe there is a method to directly access the data though, out of curiosity?
The PlayerAdded function was firing before the ui was even in the game, which is why you saw when you put task.wait() in front, it would work fine. Also, having it so anything on ui should be client sided as its just better overall for the client to control their own ui that only they will see.
The PlayerGui is something just the player can access and change. The UI is replicated to the PlayerGUI which the serverscript cannot access. Got it? Why shouldn't you change ui from the server