How do I stop this from happening?

Ok, this might not be the correct category, but whenever I try to type in 0.05 in a number value it ends up coming out like this: 0.050000000000000002776.

This code:

RE.OnServerEvent:Connect(function(plr, typeOfResearch, percent, requirement)
	local researching = plr:FindFirstChild("Researching")
	if researching ~= nil then
		local research = researching:WaitForChild(typeOfResearch)
		if research ~= nil then
			plr.leaderstats.Crystals.Value = plr.leaderstats.Crystals.Value - requirement
			research.Value = (percent / 100)
			print("Success")
		else
			error("Could not find the Type Of Research")
		end
	else
		error("Couldn't find Researching")
	end
end)

and especially here:

research.Value = (percent / 100)

does the same thing. The NumberValue will end up becoming 0.050000000000000002776 instead of 0.05. Please help me! I have no idea what is going on!

2 Likes

Are you sure that the research.Value is not being tampered on client side? It could be the issue, also see what the remoteEvent is returning before it updates the value.

EDIT: Also why go to the decimal points? You could simply have math.floor() on the values and make it go from 0 - 100 and then just add a percentage unless you are already working with decimal points for whatever reason.

1 Like

This is the entire client side.

local plr = game.Players.LocalPlayer
local re = game.ReplicatedStorage.RemoteEvents.ResearchComplete

script.Parent.Main.CanvasPosition = Vector2.new(2200, 1000)

script.Parent.Exit.MouseButton1Down:Connect(function()
	script.Parent.Visible = false
end)

for i, v in pairs(script.Parent.Main:GetDescendants()) do
	if v.ClassName == "TextButton" then
		v.Activated:Connect(function()
			if plr.leaderstats:WaitForChild("Crystals").Value >= v.Requirement.Value then
				local percent = v.Percent.Value
				re:FireServer(v.Parent.Name, percent, v.Requirement.Value)
				print("Player has enough crystals")
			elseif plr.leaderstats:WaitForChild("Crystals").Value < v.Requirement.Value then
				warn("Player does't have enough crystals")
				script.Parent.Error.Visible = true
				wait(5)
				script.Parent.Error.Visible = false
			end
		end)
	end
end

Lets say the player researches a 5% cash boost, and gets 100 cash. Then they would get 105 cash. But if you did 100 * 5, you would get 500, so…

1 Like

Floating point imprecisions. Not much you can do about it, sadly, other than working around it by making the numbers bigger.

3 Likes

You can work around this by using IntValues instead of number values. However if you want the value to be 0.05, then you would have to make your IntValue equal to 5 and then divide the value by 100 inside the script.

2 Likes

Problem is, that still won’t work…

1 Like

Nevermind, decided I’ll deal with it.

1 Like

Just a PS: in some circumstances, even if you don’t see those long-stretching decimals, they’re still there but they’ve just been truncated because they’re too small and would just clog up the view. You’ll have to ignore the decimal as that’s how NumberValue stores decimal numbers.

If you’re actually going to be displaying the number, then you can whisk away the decimals by converting the number into a string and then using %.2f with string.format. The number allows you to specify to how many decimal points a number should show.

1 Like

Maybe try

local Number = 0.050000000000000002776
local StringNum = tostring(Number)
local String = string.sub(1,3,StringNum)
local FinalNumber = tonumber(String)
print(FinalNumber)

Output = 0.05

Hope this helps!