Convert string to number

Hello, I am a beginner in scripting and I ran into a problem. I dont really know how to use tonumber. I want to convert the Text variable, which is 999999999 to a number(if I run it without doing anything it gives the error: attempt to compare string with number). I currently have this:

>local Players=game:GetService("Players")

local player=Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

local Text=script.Parent.Text

local a=tonumber(Text) --This is most likely wrong

while true do

script.Parent.Text=player.leaderstats.Money.Value		

if a>9999999999 then

Text='>1B'

end

end

This times out when the game starts. Why and how do I fix this?

​

I edited the script to this but now it gives the error attempt to compare number with nil

local Players=game:GetService("Players")

local player=Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

local Text=script.Parent.Text

while true do

if tonumber(Text)>999999999 then

	Text='1B+'

else

	Text=player.leaderstats.Money.Value

end

end

Posted Dec 25, 2019, 6:04 PM UTC · edited

112
Rinocore•

Is this what you mean?

local value = 5 -- delete "5" and get your players money

local stringvalue = tostring(value)

print(stringvalue)

1
TheArturZh•

This times out when the game starts. Why and how do I fix this?

If you make an endless loop, put wait() in it. Like so:

while true do

    wait()

    script.Parent.Text=player.leaderstats.Money.Value		
    if a>9999999999 then
        Text='>1B'
    end
end

​

attempt to compare number with nil

tonumber returns nil if string can't be converted. To be converted string should contain only numbers and "." (point). "1B+" is NOT convertible string.

Also, you can create a link to an object, but you can't do so with object's properties. The "Text" variable you created exists independently of your text container (probably it's TextLabel). If you change it, the text in your TextLabel will not change. That's why you don't see the result of the first and the only successful iteration of your while loop. Also you need to convert the number back into string, otherwise you'll get an error.

However, there is no need to convert string to number, because you already have a NumberValue in your leaderstats. Use it. The fixed code should look like this:

local Players=game:GetService("Players")
local player=Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

local Money = player:WaitForChild("leaderstats").Money

while true do
    wait()
    if Money.Value > 999999999 then
        script.Parent.Text='1B+'
    else
        script.Parent.Text=tostring(Money.Value)
    end
end

Also, it's a lot better to use an event listener attached to the player.leaderstats.Money.Value.Changed, but most probably you're not familliar with script signals. This is how it should look with an event listener:

local Players=game:GetService("Players")
local player=Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

local Money = player:WaitForChild("leaderstats").Money

Money.Changed:Connect(function(Amount)
    if Amount > 999999999 then
        script.Parent.Text='1B+'
    else
        script.Parent.Text=tostring(Amount)
    end
end)
1
Menboy321OP•

Thank you so much! I’m a beginner and this really helps, I appreciate it. Can you explain what an even listener/script signal does(and how to use them)? I assume the benefit is that it only starts when the Money.Value actually changes?

Also why do you put player:WaitForChild(‘leaderstats’).Money instead of player.Money. Value?

How do you access the .Value part if its never mentioned? (I probably have this question because I dont understand what you put in the first long paragraph)

1
TheArturZh•

it only starts when the Money.Value actually changes

Correct. Every object has it's own events, and you can use Connect to tell the game to run a certain function every time an event happens. There is a good tutorial on events on Roblox Wiki.

Also why do you put player:WaitForChild(‘leaderstats’).Money instead of player.Money. Value?

How do you access the .Value part if its never mentioned? (I probably have this question because I dont understand what you put in the first long paragraph)

Because .Value is not an object, it is a property of an object. You can't make a "link" to it.

Let's assume that A is an object, and Transparency is a property of A. If we will run this code: X = A Then X will be a link to an A. But if we'll run this code: X = A.Transparency then X will contain the value of A.Transparency (a copy of A.Transparency, in other words). If we will change A.Transparency after that, then X will not change. If we want to continiously print out a Transparency than we can do this:

while true do
    wait()
    print(A.Transparency)
end

or this:

X = A
while true do
    wait()
    print(X.Transparency)
end

But NOT this:

X = A.Transparency
while true do
    wait()
    print(X)
end

because it will always print out the same value, even is A.Transparency will change.

1
Menboy321OP•

Youre my only current help source so i hope you can answer this question for me. I want to script a car, but I cant figure out where to put the scripts. I have to use the userinputservice and need the script to be inside the model of the car(how else would I specify which player is driving which car(I dont even really know how actually, but I assume with parents)). To use the userinputservice it has to be a local script(right?), but those dont run inside a model. CAn you help me figure this out?

​

Edit: I now have this script inside a model, but it doesnt even run. I put a print in but that doesnt even output anything. Do you know whats wrong? Also, in that car the script has a line 'if player then' What does this check? If there is a player or something? And finally do you think it will work if I put a script inside the model in which I will activate a local script when someone enters the seat?

local Players = game:GetService("Players")

local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

local Seat=script.Parent.Seat

local DriveFL=script.Parent.DrivingPartLF.HingeConstraint

local DriveFR=script.Parent.DrivingPartFR.HingeConstraint

local DriveBL=script.Parent.DrivingPartLB.HingeConstraint

local DriveBR=script.Parent.DrivingPartRB.HingeConstraint

local SteerFL=script.Parent.SteeringPartLF.HingeConstraint

local SteerFR=script.Parent.SteeringPartFR.HingeConstraint

print('Test')

Seat.Changed:connect(function(property)

if property == "Occupant" then

	if Seat.Occupant=='Humanoid' then

		print('seated')

	end

end

end)

​

ALlso can you explain this part of script:

		if player then

car.DriveSeat:SetNetworkOwner(player)

local localCarScript = script.LocalCarScript:Clone()

localCarScript.Parent = player.PlayerGui

localCarScript.Car.Value = car

localCarScript.Disabled = false

1
TheArturZh•

Local Script can only run in ReplicatedFirst, Player's character, in Player instance and all it's sub instances: Backpack, PlayerScripts, PlayerGui.

'if player then' What does this check? If there is a player or something?

Any value that is not nil and not false will be interpreted by if as true. Basically if player then is the same as if player ~= nil and player ~= false then .

​

can you explain this part of script

car.DriveSeat:SetNetworkOwner(player)

The physics are simulated either on a player's computer or on a server. This line will force the physics of DriveSeat and all connected parts to be simulated by player, who sits in the seat. It eliminates the need to use RemoteEvents.

local localCarScript = script.LocalCarScript:Clone()
localCarScript.Parent = player.PlayerGui

There should be a LocalCarScript as a child of this script. It clones this localscript to PlayerGui, where it can run.

localCarScript.Car.Value = car

Initially this localscript doesn't know which car player controls, so there is an ObjectValue called "Car" as a child of this localscript. ObjectValue is an object, that can contain a link to another object. In this line of code the server-side script sets the car as a value of this ObjectValue. When "localCarScript" will start it will check the script.Car.Value to get the link to a car it should control.

localCarScript.Disabled = false

The Disabled flag prevents the script from being executed. This line of code basically enables the script.

​

Also, I think that Seat.Occupant is server-side only and can't be accessed by LocalScripts. I'm currently unable check that, but in wiki it's marked as [NotReplicated].

1
H3LLD0G3•

then Text='>1B' end end

​

attempt to compare number

player.leaderstats.Money.Value

then

player.leaderstats.Money.

do

player.leaderstats.Money.Value

wait

1
H3LLD0G3•

Uhhh feel free to ignore this cause I somehow wrote it while I was asleep and I have zero programming knowledge

1