I was trying to create a cooldown for some abilities on a weapon I created, and I got this error.
This is the code where I made the cooldown:
138 for i,v in pairs(abils:GetChildren()) do
139 if v:FindFirstChild("Cooldown") then
140 v.Cooldown.Text = v.Name == "Dive" and (tick()-var.diveTick) or v.Name == "Teleport" and (tick()-var.teleportTick)
141 v.Cooldown.Visible = v.Name == "Dive" and tick()-var.diveTick > 0 or v.Name == "Teleport" and tick()-var.teleportTick > 0
142 if v.Name == "Leap" then v.Cooldown.Visible = false end
143 end
144 end
The error is at line 140.
Let me know if you need any other information! Any help would be greatly appreciated.
You need a fallback here, if both conditions evaluate to false, the final condition will then be false, thus producing the error. So just add some random fallback like or 0 and you should be good to go. Parentheses also may or may not help as HugeCoolBoy said
v.Cooldown.Text = (v.Name == "Dive" and (tick()-var.diveTick)) or (v.Name == "Teleport" and (tick()-var.teleportTick)) or 0
Btw in Luau there is a feature called “if-then-else” expressions which act as a proper ternary operator if you’re looking for something more idiosyncratic:
v.Cooldown.Text = if v.Name == 'Dive' then tick() - var.diveTick
elseif v.Name == 'Teleport' then tick() - var.teleportTick
else 0
Thank you, this fixed the issue! I ended up recoding the part where it sets the text because it counted the opposite way I wanted it to, but you did help me!