Hey, so I don’t know why this isn’t working and I’m wondering why can anyone tell me?
–
local clickDetector = workspace.Part.ClickDetector
local ServerStorage = game:GetService("ServerStorage")
local workspace = game.Workspace
local Locker = ServerStorage.Opanables.False.Vault
local Opener = ServerStorage.Opanables.True.Vault
function onMouseClick()
if Locker.Transparency == 0.1 then
print("Vault Opened")
Locker.Transparency = 0
Opener.Transparency = 0.1
workspace.Part.CanCollide = false
else
end
if Opener.Transparency == 0.1 then
print("Vault Locked")
Opener.Transparency = 0
Locker.Transparency = 0.1
workspace.Part.CanCollide = true
end
end
clickDetector.MouseClick:connect(onMouseClick)
I tried your function, and it didn’t work. I tried it again, this time removing anything that might cause it, and also removing the parts from the folders, putting them by themselves in workspace, and naming them FalseVault and TrueVault, just to make sure ServerStorage or the folders weren’t the problem, leaving just this:
function clicked()
print("clicked")
if workspace.FalseVault.Transparency == 0.1 then
print("Vault Opened")
end
end)
workspace.Part.ClickDetector.MouseClick:Connect(clicked)
clicked got printed, but Vault Opened did not. So, I added one more line of code to make sure that the transparency of the part was set to 0.1. Even though I had set it by clicking on the little bar where it says transparency and making sure very specifically that I typed in 0.1 it still didn’t work. So I had a hunch that the transparency wasn’t working right. So, I tried this:
function clicked()
print("clicked")
print(workspace.FalseVault.Transparency)
if workspace.FalseVault.Transparency == 0.1 then
print("Vault Opened")
end
end)
workspace.Part.ClickDetector.MouseClick:Connect(clicked)
and when I ran this, I got this number:
Correct me if I’m wrong, but I think Roblox has a system where any numbers that specific get rounded. So, it should have been rounded to 0.1, and the script should have worked. If I changed the script to work if the transparency was 0, and set the part transparency to 0, it worked, and I got the normal number 0 instead of some crazy specific decimal. To be honest, I have no idea how to fix this, nor do I have any idea why this is happening. This might be a bug, but if it is, it must be a big one, since it didn’t work for both of us. You should try putting a post in engine bugs or something, but I don’t know any good way to fix this.
Ah yes, the floating point number… the disaster of many a programmer (including myself).
So basically, this happens because of how the computer handles floating-point values on a low level. I recommend you read this answer on stackoverflow for a better answer than I could every give about it, lol: Is floating point math broken? - Stack Overflow
One solution is to, instead of doing
if workspace.FalseVault.Transparency == 0.1 then
do this instead:
local tolerance = 0.0000001 -- You can change this to have more of less 0's; whatever works
if workspace.FalseVault.Transparency == 0.1 or workspace.FalseVault.Transparency - tolerance <= 0.1 and workspace.FalseVault.Transparency >= 0.1 then
This will give an area of margin to the comparison operators for when the floating point errors are apparent. When you do ==, if it’s off at all it will not work, while with the above method it should allow the 0.0000001 of “breathing room” for the floating point number.