Updating text with LocalScript

Hello, I am trying to make an upgrade system to change a player’s speed. I have something like this for now:

We have a description of the boost, and, I want it to display how much boost you get from it: (you have +x speed boost). I have a stat called “Speed” which is set to the player’s speed (16) and I want to do that it takes this stat and based on it, calculates the speed boost (local x = speed - 16)
Here’s what I have so far:

game.Players.PlayerAdded:Connect(function(plr)
	if plr:WaitForChild("Other"):WaitForChild("Gems") then
		while wait(0.5) do
			local gems = plr.Other.Gems.Value
			local speed = plr.Other.Speed.Value
			speed -= 16
			print(speed)
			script.Parent.Parent.TextLabel.Text = "Speed: Gain +2 speed everytime you buy this upgrade! (you have +"..speed.." speed boost)"
		end
	end
end)

Here is my GUI hierarchy:

image

It should print the speed value and change the text but nothing seems to happen. No printed out message and the text didn’t change…
How could I fix this? Thanks!

Do you even need the PlayerAdded event?

You could just define the player like this:

local plr = game.Players.LocalPlayer
1 Like

I dont know, but I always used PlayerAdded and it worked before so…

The if statement runs once and if it’s false, it will never run the while loop. Place the if statement inside the while loop and it should work.

small note: you aren’t decreasing the actual speed of the player in the while loop, just making sure you know that.

1 Like

It’s a local script so no need for that event, it’s un-needed and will cause bad performance when used a lot for no reason.

1 Like

Yes I know, it’s just used to know what’s the bonus of speed the player has → if he has 20 then 20-16 = 4 so he bought the upgrade 4 times

Okay, so if I use local scripts I can just do local plr = game.Players.LocalPlayer ?

That is the best way to get the player in a local script, correct.

1 Like

Yes, but, use game:GetService("Players") and save it as a variable, i personally think that’s better than doing game.Players idk if theres much of a difference but it’s a preference nothing more.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.