I want the part's Text to be the same as the game.Workspace.Gravity's value. How can I do that?

So I have this one part which has a SurfaceGUI + A TextLabel.
I want the text to be the gravity value. Meaning that game.Workspace.Gravity. I think by default it is around 196, so I would like to know how can I change the text accordingly to the gravity? For instance if the gravity is changed to 200, the text changes to 200 too?

Listen for a property change event. The Gravity property is located in the Workspace service.

local pathToTextLabel = script.Parent.SurfaceGui.TextLabel -- edit depending on your object hierarchy
workspace:GetPropertyChangedSignal("Gravity"):Connect(function()
	pathToTextLabel.Text = workspace.Gravity
end)
1 Like

For some reason, this is not working. I coped your script and added it in the Part so that it doesn’t show any error.

local pathToTextLabel = script.Parent.SurfaceGui.TextLabel – edit depending on your object hierarchy

workspace:GetPropertyChangedSignal(“Gravity”):Connect(function()

pathToTextLabel.Text = workspace.Gravity

end)

The problem is that I think this is not even working. I have no clue why, but when I ran the game in Studio the TextLabel.Text was literally nothing, just the white background. I had removed the text already expecting to see the gravity replacing the blank, but it didn’t happen. Any possible reasons why it is like this? No errors in the Output.

Oh, maybe it’s because I didn’t make it initially set the text. Does it still update when you change the gravity?

local pathToTextLabel = script.Parent.SurfaceGui.TextLabel -- edit depending on your object hierarchy
pathToTextLabel.Text = workspace.Gravity
workspace:GetPropertyChangedSignal("Gravity"):Connect(function()
	pathToTextLabel.Text = workspace.Gravity
end)

No, I changed the gravity but it had no effects.

Hmm I guess Workspace properties don’t fire a Changed event when they are changed.

Perhaps you could just try using a simple loop that updates the text periodically:

local pathToTextLabel = script.Parent.SurfaceGui.TextLabel -- edit depending on your object hierarchy
while (true) do
	pathToTextLabel.Text = workspace.Gravity
	wait(0.5) -- checks for changes every half second. doesn't really need to be any faster than this
end
1 Like

Very weird stuff is happening. When the game starts the text is changed, but when I changed it while inside the game it had no effect whatsoever. Even though I printed something right after the wait in the while loop to see if it was being executed or not. It was being executed, yet it still had no effects. Could it be a Studio bug?

Are you making the change on the server or on the client? You need to make the changes on the server in order for it to work.

3 Likes