I’m trying to create a stage selector for my obby where players can go back stages if they would like. However, my issue is that the GUI does not update when the player reaches a new stage.
[view comments below to see what is/isn’t working]
local LocalPlayer = game.Players.LocalPlayer
-- this part works
while wait() do
script.Parent.CurrentStage.Text = LocalPlayer.TeleportedStage.Value
end
-- this part does NOT work; text does not change
while wait() do
LocalPlayer.leaderstats.Stage.Value.Changed:Connect(function()
LocalPlayer.TeleportedStage.Value = LocalPlayer.TeleportedStage.Value + 1
LocalPlayer.TeleportedStage.Value = LocalPlayer.leaderstats.Stage.Value
end)
end
TeleportedStage is the value that is displayed on the GUI. When a player reaches a new stage, it should add +1 to the value, which changes the GUI as well. However it does not change the GUI.
The first loop is non-terminating because of a hack. Therefore code under it won’t get to execute.
You will want to listen for the Changed event here anyway; you were really close to doing that correctly in the second loop.
local Players = game:GetService("Players") -- obtain all services using game:GetService
local LocalPlayer = Players.LocalPlayer
local leaderstats = LocalPlayer:WaitForChild("leaderstats")
local Stage = leaderstats:WaitForChild("Stage")
local TeleportedStage = LocalPlayer:WaitForChild("TeleportedStage")
local CurrentStageLabel = script.Parent:WaitForChild("CurrentStage")
TeleportedStage.Changed:Connect(function(new)
script.Parent.CurrentStage.Text = new
end)
Stage.Value.Changed:Connect(function()
LocalPlayer.TeleportedStage.Value = Stage.Value
end)
Actually this is unnecessary, since ValueBase instances use a non-inherited Changed event which only fires when the Value property changes, and the event listeners for it get the new Value as an argument.
I was just explaining How GetPropertyChangedSignal works so he could use it in the future to detect changing of properties. But what you made is a good point too !
I have another issue - for some reason when you click the arrows the TeleportedStage value gets stuck at 99 and you have to rejoin for it to work again. Is there something wrong?
local back = script.Parent:WaitForChild("CurrentStage"):FindFirstChild("Back")
local forw = script.Parent:WaitForChild("CurrentStage"):FindFirstChild("Forward")
local chartorso = LocalPlayer.Character:WaitForChild("Torso")
forw.MouseButton1Click:Connect(function()
if LocalPlayer.TeleportedStage.Value < Stage.Value then
LocalPlayer.TeleportedStage.Value = LocalPlayer.TeleportedStage.Value + 1
chartorso.CFrame = CFrame.new(workspace.TelePoints[TeleportedStage.Value].Position)
end
end)
back.MouseButton1Click:Connect(function()
if LocalPlayer.TeleportedStage.Value <= Stage.Value then
LocalPlayer.TeleportedStage.Value = LocalPlayer.TeleportedStage.Value - 1
chartorso.CFrame = CFrame.new(workspace.TelePoints[TeleportedStage.Value].Position)
end
end)
The issue is with your If conditions I assume . I guess your LocalPlayer.TeleportedStage.Value will be 100 , so when your Stage.Value Becomes 99
100 <= or < 99 wont work , so do if LocalPlayer.TeleportedStage.Value > Stage.Value
This should work .
Well try removing the if condition where you reduce the Stage.Value , that is not really needed and replace it with if 0 < Stage.Value This Prevents it going below 0.