Script Not Loading Without Task.Wait(10)

  1. What do I want to achieve: Faster loading time of Scripts

  2. 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.

  3. 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)


4 Likes

Have you tried using player.CharacterAdded? From my guess, the ui isn’t loaded when the playeradded event is fired.

3 Likes

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?

2 Likes

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?

3 Likes

It did not work. For some reason just the level stats(not word) and xP values are missing from the playerData if that helps.

2 Likes

I just realized, why are you doing the ui server sided? Is there a reason for it to be server sided?

2 Likes

Yes, I have to do it server sided because I am using profile service and my Manager is in ServerScriptService.

3 Likes

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.

3 Likes

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?

2 Likes

mm that is strange I’ve never seens something like that, uhh.

2 Likes

What about changing the While loop to a RunService?

2 Likes

PETPWER’s solution worked, so I doubt that would work and frankly don’t want to try. If anyone else thinks this works though let me know.

2 Likes

You seem knowledgeable though. Why was this the reason for the issue? You can’t access the PlayerGui on the server?

2 Likes

Changing that to runservice is still better.

2 Likes

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.

1 Like

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

1 Like

so playergui can only be accessed by the client? or can it be accessed by server at all?

1 Like

PlayerGui can be access by the server.

PlayerGui can be accessed by the server.

oh ok so its because player added was firing before the ui loaded?

1 Like