Unable to assign property Text. string expected, got boolean

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.

Try changing this snippet to this:

v.Cooldown.Text = (v.Name == "Dive" and (tick()-var.diveTick)) or (v.Name == "Teleport" and (tick()-var.teleportTick))

Parentheses matter in situations such as this

1 Like

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
1 Like

I will make sure to remember this information, thank you!

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!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.