Tonumber doesn't work in local script

  1. What do you want to achieve? Keep it simple and clear!

I want to change a string into a number

  1. What is the issue? Include screenshots / videos if possible!

When I run the code I get this error

Players.Thedarkvoidlurks.PlayerGui.Local game manger:26: attempt to compare number <= Instance

local script

for i, item in pairs(game.Workspace.House.Shop.SurfaceGui.Frame.NormalShop:GetChildren()) 
	if item:FindFirstChildWhichIsA("TextButton") then
		item:FindFirstChildWhichIsA("TextButton").MouseButton1Click:Connect(function()
			local price = item:FindFirstChildWhichIsA("TextButton").Name --the name of the text button is 1--
			if money >= tonumber(price) then --Error--
				if item.Name == "Food" then
					money -= tonumber(price)
				end
				game.ReplicatedStorage["Money transfer"]:FireServer(money)
			else
				print("Not enough money")
			end
		end)
	end
end
1 Like

If you look at the error message, you’ll see that one of the operands is an Instance, in your case it’s the right hand side of <= (the left hand side of >=).

In Luau, the comparison operation will error on different type and different metamethods.

So my variable money is an Instance?

Yes. It’s an Instance; without information I guess it’s either IntValue or NumberValue, in that case you can use money.Value instead of money.

I tried using money.Value but it gave me

Players.Thedarkvoidlurks.PlayerGui.Local game manger:26: attempt to index number with 'Value'
1 Like

Where is the money local variable first assigned? I’d guess the variable money has changed from IntValue/NumberValue Instance to number.

Here is my local script I can’t seem to find where my variable changes

---Varibles---

local shopframe = game.Workspace.House.Shop.SurfaceGui.Frame

local shop = game.Workspace.House.Shop

local money = 0

local Newpositon = game.Workspace.House.Newshop.CFrame
local oldposition = CFrame.new(-17.6289692, 4.67552853, 20.9015789, 1, -0, 0, 0, 0.984812498, 0.173621148, -0, -0.173621148, 0.984812498)
local Tweenservice = game:GetService("TweenService")
local partTween = Tweenservice:Create(shop, TweenInfo.new(2, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out, 0, false, 0), { Size = Vector3.new(9, 4, 0.3), CFrame = Newpositon})
local partTweenreverse = Tweenservice:Create(shop, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), { Size = Vector3.new(3, 1.6, 0.3), CFrame = oldposition})

--test--

for i, item in pairs(game.Workspace.House.Shop.SurfaceGui.Frame.NormalShop:GetChildren()) do --Buying things in the shop--
	if item:FindFirstChildWhichIsA("TextButton") then
		item:FindFirstChildWhichIsA("TextButton").MouseButton1Click:Connect(function()
			local price = item:FindFirstChildWhichIsA("TextButton").Name
			if money >= tonumber(price) then
				if item.Name == "Food" then
					money -= tonumber(price)
				end
				game.ReplicatedStorage["Money transfer"]:FireServer(money)
			else
				print("Not enough money")
			end
		end)
	end
end


---Code---



for i, v in pairs(game.Workspace.House.Laptop:GetChildren()) do --Shop for each player--
	local clickdetecter = Instance.new("ClickDetector")
	clickdetecter.Parent = v
	v.ClickDetector.MouseClick:Connect(function()
		partTween:Play()
		shopframe.Visible = true
	end)
end
game.Workspace.House.Shop.SurfaceGui.Frame.Close.MouseButton1Click:Connect(function() --Shop for each player--
	partTweenreverse:Play()
	wait(0.2)
	shop.SurfaceGui.Frame.Visible = false
end)

game.ReplicatedStorage["Money transfer"].OnClientEvent:Connect(function(transfermoney) --Money transfered from server to each client--
	money = transfermoney
end)



while true do 
	wait()
	script.Parent.ScreenGui["Money gui"].Text = "$: "..money
end

It seems like the problem is in here, because this block below me used to be in a server script but when I added it to my local script it seems to change my variable into an instance

for i, item in pairs(game.Workspace.House.Shop.SurfaceGui.Frame.NormalShop:GetChildren()) do --Buying things in the shop--
	if item:FindFirstChildWhichIsA("TextButton") then
		item:FindFirstChildWhichIsA("TextButton").MouseButton1Click:Connect(function()
			local price = item:FindFirstChildWhichIsA("TextButton").Name
			if money >= tonumber(price) then
				if item.Name == "Food" then
					money -= tonumber(price)
				end
				game.ReplicatedStorage["Money transfer"]:FireServer(money)
			else
				print("Not enough money")
			end
		end)
	end
end

So it turns out when the client talks to the sever the server script turns it into a Instance

client

game.ReplicatedStorage["Money transfer"]:FireServer(money)

server

game.ReplicatedStorage["Money transfer"].OnServerEvent:Connect(function(transfer)

money = transfer

game.ReplicatedStorage["Money transfer"]:FireAllClients(money)

end)

The reason for this is because the player who fired the remote is always automatically passed as the first argument when firing to the server, so your server-side setup was supposed to look like this:

game.ReplicatedStorage["Money transfer"].OnServerEvent:Connect(function(player, money)
1 Like

I know I’m late but I just added that setup to my game and the problem is fixed thanks!