Transparency doesn't change correctly

local RemoteEvent = Instance.new("RemoteEvent", script.Parent)
RemoteEvent.Name = "Transparency"

RemoteEvent.OnServerEvent:Connect(function(player, part)
	if part.Parent == workspace then
		if part.Locked == false then
			if part.Transparency == 1 then
				part.Transparency = 0
			else
				part.Transparency = part.Transparency + 0.1
			end
		end
    end
end)

I’m making BTools for my game, however this transparency script doesn’t check if transparency is already 1 and just keeps raising it.

Trying creating the RemoteEvent beforehand by placing it in the ReplicatedStorage and then defining it.

local replicatedstorage = game:GetService("ReplicatedStorage")
local RemoteEvent = replicatedstorage:FindFirstChild("Transparency")

I feel like I’ve heard recently that the engine is having floating point errors sometimes, perhaps this is an example of it. Instead of transparency == 1, try >=

1 Like

because numbers in Lua 5.1 are double, I’d to >= or > for this, as double can’t accurately represent values.

#include <stdio.h>

int main()
{
    for (double i; i < 1; i += 0.1)
    {
        printf("%.17f", i);
    }

    return 0;
}

prints

0.00000000000000000 0.10000000000000001 0.20000000000000001 0.30000000000000004 0.40000000000000002 0.50000000000000000 0.59999999999999998 0.69999999999999996 0.79999999999999993 0.89999999999999991 0.99999999999999989

So you’re comparing 1 to 0.99999999999999989

Unless you’re also doing this for Lua 5.3 (or vanilla Lua), you could do, to save typing

part.Transparency += 0.1

You can do

not part.Locked

To add to that, Lua 5.1 numbers cannot represent values over 2 ^ 53 safely, also float is ambiguous, it can refer to single (32-bit) or double (64-bit), Lua 5.1 uses double.
For more information see: https://0.30000000000000004.com/