Player Gui Stage number dosen't changing

script:
line 146

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local ObbyDataStore = DataStoreService:GetDataStore("ObbyDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SkipStage = ReplicatedStorage:WaitForChild("SkipStage")
local ResetStage = ReplicatedStorage:WaitForChild("ResetStage")


local Checkpoints = workspace:WaitForChild("Checkpoints")
local inGameStartupPlayers = {}
local CurrentStage = {}
local TouchDb = {}

local ProductId = 1135733177 -- Change to your developer product id

local function NewCharacter(player, char)
	local TempCurrentStage = CurrentStage[player.UserId]
	if TempCurrentStage ~= nil then
		local TempCheckpoint = Checkpoints:FindFirstChild(TempCurrentStage)
		if TempCheckpoint ~= nil then
			repeat wait(0.1) until char.PrimaryPart ~= nil
			char:SetPrimaryPartCFrame(CFrame.new(TempCheckpoint.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(TempCheckpoint.Orientation.Y) + math.rad(90), 0))
		end
	end
end

local function NewPlayer(player)
	local success, stage = pcall(function()
		return (ObbyDataStore:GetAsync(player.UserId)) or 1
	end)

	CurrentStage[player.UserId] = stage

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Stage = Instance.new("IntValue", leaderstats)
	Stage.Name = "Stage"
	Stage.Value = stage

	local TempChar = player.Character
	if TempChar ~= nil then
		NewCharacter(player, TempChar)
	end
	player.CharacterAdded:Connect(function(char)
		NewCharacter(player, char)
	end)
end

Players.PlayerAdded:Connect(function(player)
	if inGameStartupPlayers[player] == nil then
		NewPlayer(player)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local success = pcall(function()
		ObbyDataStore:SetAsync(player.UserId, CurrentStage[player.UserId])
	end)
	CurrentStage[player.UserId] = nil
end)

SkipStage.OnServerInvoke = function(player)
	local connection
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats ~= nil then
		local Stage = leaderstats:FindFirstChild("Stage")
		if Stage ~= nil then
			if #Checkpoints:GetChildren() ~= Stage.Value then
				local PurchaseResult = "Purchase Failed"
				connection = MarketplaceService.PromptProductPurchaseFinished:Connect(function(userId, productId, purchased)
					if player.UserId == userId and productId == ProductId then
						if purchased == true then
							PurchaseResult = "Success"
						end
					end
					connection:Disconnect()
				end)
				MarketplaceService:PromptProductPurchase(player, ProductId)
				repeat wait(0.1) until connection.Connected == false or Players:GetPlayerByUserId(player.UserId) == nil
				return PurchaseResult
			else
				return "You have reached the highest stage!"
			end
		end
	end
end

ResetStage.OnServerEvent:Connect(function(player)
	local TempLeaderstats = player:FindFirstChild("leaderstats")
	if TempLeaderstats ~= nil then
		local TempStage = TempLeaderstats:FindFirstChild("Stage")
		if TempStage ~= nil then
			if TempStage.Value >= 2 then
				
				CurrentStage[player.UserId] = CurrentStage[player.UserId] -1
				TempStage.Value = TempStage.Value -1

			end
		end
	end     
	NewCharacter(player, player.Character)
end)

MarketplaceService.ProcessReceipt = function(recieptInfo)
	if recieptInfo.ProductId == ProductId then
		local player = Players:GetPlayerByUserId(recieptInfo.PlayerId)
		if player ~= nil then
			CurrentStage[player.UserId] = CurrentStage[player.UserId] + 1
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats ~= nil then
				local Stage = leaderstats:FindFirstChild("Stage")
				if Stage ~= nil then
					Stage.Value = CurrentStage[player.UserId]
				end
			end
			local TempChar = player.Character
			if TempChar ~= nil then
				NewCharacter(player, TempChar)
			end
			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

for i,v in pairs(Checkpoints:GetChildren()) do
	local StageNum = tonumber(v.Name)
	v.Touched:Connect(function(hit)
		local char = hit.Parent
		if char ~= nil then
			local Humanoid = char:FindFirstChildOfClass("Humanoid")
			if Humanoid ~= nil and Humanoid.Health > 0 then
				local player = Players:GetPlayerFromCharacter(char)
				if player ~= nil and (TouchDb[player.UserId] or 0) + 1 <= os.time() then
					TouchDb[player.UserId] = os.time()
					local TempCurrentStage = CurrentStage[player.UserId]
					if TempCurrentStage == StageNum - 1 then
						CurrentStage[player.UserId] = StageNum
						local TempLeaderstats = player:FindFirstChild("leaderstats")
						if TempLeaderstats ~= nil then
							local TempStage = TempLeaderstats:FindFirstChild("Stage")
							if TempStage ~= nil then
								TempStage.Value = StageNum
								player.PlayerGui:WaitForChild("MainGameUI").Main.StageNumber.TextLabel.Text = StageNum.Value
							end
						end
					end
				end
			end
		end
	end)
end

inGameStartupPlayers = Players:GetPlayers()
for i,v in pairs(inGameStartupPlayers) do
	spawn(function()
		NewPlayer(v)
	end)
end

game:BindToClose(function()
	for i,v in pairs(Players:GetPlayers()) do
		ObbyDataStore:SetAsync(v.UserId, CurrentStage[v.UserId])
	end
	wait(1)
end)

inGameStartupPlayers = {}Preformatted text

i want the gui will change the stage number when player reached next level or backword level.

something not working here and i want to undarstand what it is

here the line:
“player.PlayerGui:WaitForChild(“MainGameUI”).Main.StageNumber.TextLabel.Text = StageNum.Value”

Please learn english or at least try to use google translate, because I literally cant understand anything you said.

Im assuming you got a error in the line you mentioned, but you do not say what error it was. Could you tell us what error message it was?

1 Like

there is no error, i wrote that line in my script to make the “StageNumber” Gui change its value to the current stage Value and the issue is that there is nothing happening, nothing change(in the gui),

Updating a gui requires you to use a local script, since gui’s are replicated among clients (players), so probably add some code in a local script that deals with this.

i will try and tell you if it worked

1 Like

LOL, I’m not sure this dude is from an English speaking country. Does need to redact better though.

its not working still nothing happening can you make me example script?

i will undarstand better

Yes. First, no need to erase the original server script. You can erase the line in the server that deal with updating the gui though.

in the local script, try writing:

local Gui = script.Parent -- If the script is parented in the gui
local Number = -- Stage number value, like the leader stat value
--or 
local Text = --Text

Number.Changed:Connect(function()
   Gui.Text = Number.Value
end)

--or in the other case

Text.Changed:Connect(function()
   Gui.Text = Text.Text
end)

Tweening the gui also goes in the local script. but make sure to a have a trigger when a property of your stage number or text changes, and the way to update that info in your gui.

local Gui = script.Parent -- If the script is parented in the gui
local Number = game.Players.LocalPlayer.leaderstats.Stage.Value
	--or 

Number.Changed:Connect(function()
	
Gui.Text = Number.Value
end)

image

i did that and its erroring that attepmt to index number with changed

Erase the .Value from the end of your number variable

it should be:

local Gui = script.Parent -- If the script is parented in the gui
local Number = game.Players.LocalPlayer.leaderstats.Stage

Number.Changed:Connect(function()
	
Gui.Text = Number.Value
end)
1 Like

Oh sorry, this a leaderstat.

local Stage = game.Players.localplayer.Leaderstats:WaitforChild("StageNumber")
local Gui = script.Parent

function Update()
   Gui.text = Stage.Value
end

Stage.Changed:Connect(Update)
1 Like

yes i did that and it work but now when player exiting the game and joining the gui value is 0 untill he touch the next checkpoint.

for example the player was stage 6 and the left the game after time he back and he still stage 6(because i have datasave) but the gui showing 0

You we’re right it’s just that he added .Value when for the changed event it doesn’t use .Value

You need to use datastore to save the leaderstats data

i have already but the gui not saving the data

the stage is saving and when player come back to game he still in the same stage he was last time but the gui’s text is 0 untill he touch other checkpoint and then the number on the gui will be the stage number

So add this to your local script:

game.Players.PlayerAdded:Connect(function(player)
Gui.Text = player:WaitForChild("leaderstats"):WaitForChild("Stage").Value
end)


first image is when the player only spawned

the second one is after he touched the next stage

This will make it so it will update the text to the players stage number when they join