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

Script not grabbing value from a NumberValue?

Hello, making a GUI that show's a current song. I made it so the ID song will get in a NumberValue so I can do if ID == "123" Text.Text = "Song" But it's not changing the text. Halp me pl0x.

local TEXT = script.Parent.Frame.TextLabel
local ID = game.Workspace.MusicPlayer:FindFirstChild("ID")


while true do
	wait(0.1)
	if ID.Value == "467472854" then
	TEXT.Text = "SONG"

end	
end

1 comment

0

You are trying to compare a NumberValue to a String, which you cannot do. Simply get rid of the quotes around the String with the ID, and your code should work.

1 answer

✓ Accepted answer

#The problem You're comparing a NumberValue with a StringValue, the if then statement will never fire because Numbers cannot be associated with strings.


#tonumber and tostring You can use tostring() to turn the string to a number or use tonumber() to turn the number to a string.

print(type(tostring(2))) --Prints the type of value
print(type(tonumber("2+2"))) --Prints the type of value

string

number


#Final Product

local TEXT = script.Parent.Frame.TextLabel
local ID = game.Workspace.MusicPlayer:FindFirstChild("ID")


ID.Changed:connect(function() --Used the Changed event instead so you don't have to use infinite loops, those things cause lag.
	if ID.Value == 467472854 then --Just erased the quotes to make it a number
		TEXT.Text = "SONG"
	end 
end)


Hope it helps!

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