So this is my script:
local player = game.Players.LocalPlayer local tokens = player.leaderstats.Tokens script.Parent.MouseButton1Click:Connect(function() if tonumber(script.Parent.Text) > game.ReplicatedStorage.CurrentBid.Value or game.ReplicatedStorage.CurrentBid.Value == nil then if tonumber(script.Parent.Text) <= tokens.Value then game.ReplicatedStorage.NewBid:FireServer(tonumber(script.Parent.Text)) end end end)
For some reason i get the error "attempt to compare number with < nil" which is really odd. I have done some investigating and it seems as if when you enter text into the text box and press enter, it doesnt register in the "Text" property of the textbox.
Help appreciated.
Guessing that you're talking about tonumber(script.Parent.Text) <= tokens.Value
as you've mentioned <
in the error. This happens when script.Parent.Text
is not a number, in which case tonumber(script.Parent.Text)
will return nil
.
To fix this, I recommend simply adding a check if it is a number or not and cancelling the function if it isn't:
local player = game.Players.LocalPlayer local tokens = player.leaderstats.Tokens script.Parent.MouseButton1Click:Connect(function() if tonumber(script.Parent.Text) == nil then return end -- Usually I'd recommend doing `not tonumber(script.Parent.Text)` instead of `tonumber(script.Parent.Text) == nil`, but for demonstration purposes I used the earlier instead. if tonumber(script.Parent.Text) > game.ReplicatedStorage.CurrentBid.Value or game.ReplicatedStorage.CurrentBid.Value == nil then if tonumber(script.Parent.Text) <= tokens.Value then game.ReplicatedStorage.NewBid:FireServer(tonumber(script.Parent.Text)) end end end)
Your script may load and run even before the GUIs, Frames, Textboxes, etc. So I recommend using
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:Wait() repeat wait() until player.Character --From my experience this usually buffers the scripts just enough for the datastore to load it. local tokens = player:WaitForChild('leaderstats'):WaitForChild('Tokens')
Another method you may do to prevent fast loads, (though may cause a delay per click by I guess around 0.00001s depending on your processor)
Is simply to put the 'local tokens = player.leaderstats.Tokens' inside the function.