Roblox not checking when bool value is updated? - boolean

So I've been trying to make a script on Roblox where tubes turn black when a Boolean is changed to false but it doesn't detect when it becomes false.
local Coolant = workspace.Temperature.CoolantActive
local CoolTube = workspace.TheCore.Coolant.Cool1
while true do
if Coolant.Value == true then
CoolTube.Material = ("Neon")
CoolTube.BrickColor = BrickColor.new("Toothpaste")
elseif Coolant.Value == false then
CoolTube.Material = ("Metal")
CoolTube.BrickColor = BrickColor.new("Really black")
end
wait(1)
end
I have no idea why this is happening, help?!

Rather than having a loop run forever, try using the Changed signal to detect when the Value changes.
local Coolant = workspace.Temperature.CoolantActive
local CoolTube = workspace.TheCore.Coolant.Cool1
Coolant.Changed:Connect(function(newValue)
if newValue == true then
CoolTube.Material = Enum.Material.Neon
CoolTube.BrickColor = BrickColor.new("Toothpaste")
else
CoolTube.Material = Enum.Material.Metal
CoolTube.BrickColor = BrickColor.new("Really black")
end
end)

Related

How do I print the boolean value?

Okay, so I'm currently trying to write the code for a very inaccurate stoplight--one that is only meant to help me understand what I've learned, but I can't seem to figure it out! In my code, at the very end, I say print("Green Light!") after setting up a few variables, but I would like to indicate what the computer should do instead of flat out saying "print this phrase," if that makes sense... not sure it does.
How would I write this if I wanted to simply print the Boolean value of greenLight without saying print("Green Light!")?
I'm very much a beginner and I might be asking the wrong question--I know that, but I'm hoping someone can help!
Something tells me I haven't learned enough to do this, yet, but I really wanna know how this works.
This is what I've written so far. It runs, but I would like to change it so all I have to say is print(greenLight) or print(Bool).
When I try putting in print(greenLight), it returns an error:
Output:
Review.swift:14:7: error: variable 'greenLight' used before being initialized
print(greenLight)
^
Review.swift:4:5: note: variable defined here
var greenLight: Bool
^
var carAtRightIntersection = false
var carAtLeftIntersection = false
var carStraightAhead = true
var greenLight: Bool
if !(carAtRightIntersection && carAtLeftIntersection) && carStraightAhead {
greenLight = true
}
if carAtRightIntersection && carAtLeftIntersection && !(carStraightAhead) {
greenLight = false
}
print("Green light!")
Edit: I consulted a few coding friends, and they provided a very good solution! Provided here:
var carAtRightIntersection = false
var carAtLeftIntersection = false
var carStraightAhead = true
var colorOfLight: String = "Red"
if !(carAtRightIntersection && carAtLeftIntersection) && carStraightAhead {
colorOfLight = "Green"
}
/*
if carAtRightIntersection && carAtLeftIntersection && !(carStraightAhead) {
greenLight = false
}
*/
print(colorOfLight + " light :)")
If you don't want to print the actual phrases, You could use a switch case statement
switch greenLight{
case true:
print("Green Light!")
case false:
print("Red Light!")
default:
print("Yellow Light!")
}
After the first block executes

My Teleport Script Always Teleports You When You Say Something and I Don't Want It To Do That

Every time I say a single word or letter it will always teleport me even though I don't want it to. I just want me to teleport me when I say what I put in it.
Here is the code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(raw_msg)
local msg = raw_msg:lower()
if msg == ":cmds" or ";cmds" or "/cmds" or ":cmd" or ";cmd" or "/cmd" or
":commands" or ";commands" or "/commands" or ":command" or ";command" or "/command"
then
local char = player.Character
local humanroot = char:WaitForChild("HumanoidRootPart")
humanroot.Position = game.Workspace["Secret room
1"]:WaitForChild("Punish").Position + Vector3.new(0, 1, 0)
end
end)
end)
if msg == ":cmds" or ";cmds" or "/cmds" or ":cmd" or ";cmd" or "/cmd" or ":commands" or ";commands" or "/commands" or ":command" or ";command" or "/command"
This is not the correct way to test if msg is equal to any one of these multiple values. The strings themselves are not nil or false so they evaluate to true.
Try the following:
if msg == ":cmds" or msg == ";cmds" or msg == "/cmds" or msg == ":cmd" or msg == ";cmd" or msg == "/cmd" or msg == ":commands" or msg == ";commands" or msg == "/commands" or msg == ":command" or msg == ";command" or msg == "/command" then
-- Code
end
However, this can be simplified:
if ({["/"] = true, [":"] = true, [";"] = true})[msg.sub(1, 1)] and ({["cmd"] = true, ["cmds"] = true, ["command"] = true, ["commands"] = true})[msg.sub(2, -1)] then
-- Code
end

Swift charts not showing value labels

I am using the Swift Charts library by Daniel Gindi. I am trying to show individual values for each point in a scatter plot.
I have set drawValuesEnabled to true for the set and created a custom IValueFormatter. However, it appears that the method in the formatter class is never even being called, and no labels are shown. What else could be the cause of this?
EDIT: Code to create scatter plot is as follows:
let graphView: ScatterChartView = {
let scatter = ScatterChartView()
scatter.translatesAutoresizingMaskIntoConstraints = false
scatter.chartDescription?.enabled = true
scatter.dragEnabled = true
scatter.setScaleEnabled(true)
//scatter.maxVisibleCount = 200
scatter.pinchZoomEnabled = false
scatter.backgroundColor = .clear
scatter.leftAxis.enabled = false
scatter.rightAxis.enabled = true
scatter.rightAxis.drawGridLinesEnabled = false
scatter.rightAxis.valueFormatter = TimeValueFormatter()
scatter.xAxis.labelPosition = XAxis.LabelPosition.bottom
scatter.xAxis.axisMaxLabels = 8
scatter.xAxis.granularity = 1
scatter.xAxis.drawLabelsEnabled = true
scatter.xAxis.drawGridLinesEnabled = false
//date formatter should be provided by controller
scatter.legend.enabled = false
return scatter
}()
And the data set (with x-axis as integers and y-axis as time):
self.dataSet = {
let set = ScatterChartDataSet(values: entries)
set.setScatterShape(.circle)
set.setColor(UIColor.PRColors.prGreen)
set.valueFormatter = TimeValueFormatter()
return set
}()
Expected result is labels with the y-value shown by each point, as in the example project.
Figured it out. Turns out, when you create a data set using an array of ChartDataEntry objects, the array must be immutable (let rather than var), or labels will not be shown.

Code for modify DB?

I have no idea what kind of language of programming is this, or what the method does, any help?
It could be some code for modify some DB tables?
ClassMethod CreateNewConfiguration(pintInsTypeConf As %Integer) As %Integer
{
Set objRecord = ##class(Table.tInsTypeConfigurations).%OpenId(pintInsTypeConf)
Set objNewRecord = ##class(Table.tInsTypeConfigurations).%New()
Set objClassDef = ##class(%Dictionary.ClassDefinition).%OpenId("Table.tInsTypeConfigurations")
Set intTotal = objClassDef.Properties.Count()
For intCount = 1:1:intTotal
{
If (((objClassDef.Properties.GetAt(intCount).Relationship = 0) &&
(objClassDef.Properties.GetAt(intCount).Calculated = 0)) ||
((objClassDef.Properties.GetAt(intCount).Relationship = 1) &&
(objClassDef.Properties.GetAt(intCount).Cardinality = "one")))
{
Set strName = objClassDef.Properties.GetAt(intCount).Name
Set $zobjproperty(objNewRecord,strName) = $zobjproperty(objRecord,strName)
}
}
Set objNewRecord.Name = objNewRecord.rInstrumentTypes.%Id() _ " Config B “
Set intResult = objNewRecord.%Save()
If ((intResult '= 1) || ($ZERROR '= ""))
{
Quit 0
}
Quit objNewRecord.%Id()
}
yes, #duskwuff is right, it is Caché ObjectScript code.
And in this code, just make a copy properties for some object, from class Table.tInsTypeConfigurations with id pintInsTypeConf, to new object. It is not optimized code, but anyway, it should does this task.

Best way to check for all values in a Int

I don't know if there is a better way of doing this, my solution works but just seems to repetitive.
I have a life indicator which is represented using an icon within SpriteBuilder, these are set to not visible, and then within source I have implemented the following code:
lives = 3 // this is reduced or increased within game
if (lives == 5) {
lifeIcon1.visible = true
lifeIcon2.visible = true
lifeIcon3.visible = true
lifeIcon4.visible = true
lifeIcon5.visible = true
} else if (lives == 4) {
lifeIcon1.visible = true
lifeIcon2.visible = true
lifeIcon3.visible = true
lifeIcon4.visible = true
} else if (lives == 3) {
lifeIcon1.visible = true
lifeIcon2.visible = true
lifeIcon3.visible = true
} else if (lives == 2) {
lifeIcon1.visible = true
lifeIcon2.visible = true
} else if (lives == 1) {
lifeIcon1.visible = true
}
This works fine but just seems repetitive, and is difficult to scale if I wanted to increase the lives to more than 5 at a later date.
Any suggestions?
Assuming you want to set your visible to false when you decrease your lives, you can use something like this:
let lifeIcons = [lifeIcon1, lifeIcon2, lifeIcon3, lifeIcon4, lifeIcon5]
...
for (index, lifeIcon) in lifeIcons.enumerate() {
lifeIcon.visible = index < lives
}
Or if you like using closures:
lifeIcons.enumerate().forEach{$1.visible = $0 < lives}
(Here $1 is the current life icon in the iteration, and $0 is the index of that icon)
The key point here is to use an array, instead of multiple variables to keep track of your life icons. You can then iterate through this array, using the indices of the elements in order to determine which icon they are. enumerate() is used in order to create a lazy sequence of pairs of elements with their indices.
In this case we can compare the indices of the elements with the current number of lives. Because indices are 0 based, we can check whether a given index is less than the current number of lives in order to determine whether it should be visible.
How about this:
let icons = [lifeIcon1, lifeIcon2, lifeIcon3, lifeIcon4, lifeIcon5]
icons[0..<lives].forEach { $0.visible = true }
It is similar to #Adam Heeg's answer, but the c-style loops are in the process of being deprecated. I would slice the array by subscript and use forEach instead.
It seems like you have some other code which you don't wnat to change. I would suggest the following.
Load all your lifeIcon's into an array then do the following. (adjust for if you are 0 based or not).
for (int i = 0; i < lives; i++)
{
lifeIconArray[i].visible = true;
}