Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help on this GUI text script?

I have a script that is meant to display the text of a TextLabel as an IntValue is a player. It is in the PlayerGUI as is the Apples IntValue. (Image here: http://mystdar-roblox.deviantart.com/art/Apples-487189581?ga_submit_new=10%253A1412791523)

The script is like this

while wait(.1) do
local apple_count = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Apples
script.Parent.Text= "Apples:" .. apple_count.Value 
end

I aim to make it only fire/occur when the value is changed, if you could help with that too, that would be great. Thanks.

3 answers

✓ Accepted answer

Try making your code more efficient. You could easily just use game.Players.LocalPlayer instead of using the Parent property repetitively (like Spectrobz and MrSmenry have kept it as.)

Also, you can use the Changed event to check when a property changes in an Instance (in this case, the Value property.)

Try using this in a LocalScript:

local player = game.Players.LocalPlayer --The local player.
local apples = player:WaitForChild("Apples") --Yields (makes the script wait) until the apples object becomes available.

apples.Changed:connect(function() --Connects a function to an event. Known as an anonymous function (as it has no direct identifier.)
	script.Parent.Text = "Apples: " .. apples.Value --Sets the Text property of the script's parent to "Apples: " then the apples variable's value. The two dots are used for string concatenation (merging of values to form a string.)
end) --Ends the anonymous function.

6 comments

0

Is apples:Changed meant to be apples.Changed?

0

This one works, thanks, one small error, (on my part) the script only fires when it is changed, so until the unit is changed it just says Apple: Any help?

1

Yeah, good eyes. Ha ha. Also, just set the Text property of the script's parent to "Apples: 0" (without the quotes) initially. 

0

That would only work if their one was 0, I intent to save this Int so if someone rejoined and had 20 apples, then it wouldn't be one, yes? Thanks for the compliment, btw.

0

Yeah, copy line 5 to line 3 so that it sets the text property to the Apples value before the Changed event occurs.

0

Alright, thanks.

Here you go ;)

local apples = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Apples

apples.Changed:connect(function()
script.Parent.Text= "Apples:" .. apples.Value
end)

2 comments

0

Doesn't work for me, I just get this: http://mystdar-roblox.deviantart.com/art/Apple-GUI-487194079?ga_submit_new=10%253A1412793106 The GUI's normal text is Apples: And it doesn't work/change it.I have tried your script in a local one and normal

0

Did you check your output window for errors? Did you check if the text wasn't blank in case you had it hidden or something?

while wait(.1) do
	local apple_count = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Apples
	script.Parent.Text = "Apples:" .. apple_count.Value 
	
	apple_count.Changed:connect(function()
		script.Parent.Text = "Apples:" ..apple_count.Value
	end)
end

If this does not work, please contact me any errors you receive.

This discussion is preserved as it was. New answers, comments, and votes are closed.