roblox lua script not activating - scripting

so i have this code
local player = game.Players.LocalPlayer
local unitFrame = script.Parent
local buttona = unitFrame.buttonA
local sprinting = true
unitFrame.Title.Text = "Perks"
local function onButtonAClick()
unitFrame.Title.Text = "perks"
end
local function sprintButton()
if sprinting == false then
sprinting = true
player.Character.Humanoid.WalkSpeed = 20
unitFrame.buttonA.Text = "strinting"
end
if sprinting == true then
sprinting = false
player.Character.Humanoid.WalkSpeed = 16
unitFrame.buttonA.Text = "walking"
end
end
buttona.MouseButton1Click:connect(sprintButton)
what i am trying to do is make a sprinting toggle program. The only problem is that it will work once then not work at all. i can click it and it changes the text then when i click it again it does nothing. i want it to be able to work every time you press it.

In the function you test 2 if statements. If sprinting is false, and if it it is true.
The key to understanding your problem is that your if statements counteract eachother.
Let's walk through this, first it detects if it's false. No, sprinting is not false. Then if checks if it's true. It is! So it sets it to false.
Great so far, right? Well here's your problem. When you try it again, sprinting is false. So the first if statement is run, and it sets it to true. But then, ANOTHER if statement is run immediately after. Since it's now true, this runs, setting it to false. It appears nothing happened.
Your solution? Take out the end to the first if statement, and replace the second if to an elseif. This way once one statement evaluated to true, it won't evaluate the next.

Your logic is not correct. As warspyking said, you have an issue with your conditionals. Fix this by adding an elseif statement instead.
local function sprintButton()
if not sprinting then
sprinting = true
player.Character.Humanoid.WalkSpeed = 20
unitFrame.buttonA.Text = "sprinting"
elseif sprinting then
sprinting = false
player.Character.Humanoid.WalkSpeed = 16
unitFrame.buttonA.Text = "walking"
end
end

Related

How can I simplify and enhance multiple if-statements usage?

So I'm just curious and trying to improve my current application. Couldn't really find any examples of this.
I've got a bunch of checkboxes cbcin my form and I want to do something if none of them is checked.
Is there any better ways to do this?
If Not cbc1.Checked = True Then
If Not cbc2.Checked = True Then
If Not cbc3.Checked = True Then
If Not cbc4.Checked = True Then
If Not cbc5.Checked = True Then
If Not cbc6.Checked = True Then
If Not cbc7.Checked = True Then
If Not cbc8.Checked = True Then
If Not cbc9.Checked = True Then
'Do this and that
End If
End If
End If
End If
End If
End If
End If
End If
End If
I find this a bit messy and its probably not a good way to write a good code.
Why not use .All() from LINQ? It's cool (and readable)
Dim checkBoxes As New List(Of CheckBox) From {cbc1,cbc2,cbc3} 'all your check boxes
If checkBoxes.All(Function(cb) Not cb.Checked) Then
'Do this and that
End If
Also you don't need to do if boolObj = True Then. Just if boolObj Then is fine.
Edit: The condition was the other way round. Fixed now.
Since you aren't doing anything, except in your last If statement, why not use AND operator and combine all the boolean checks ?
That way you will only have one if then end if loop.

Make image visible or not visible based on multiple checkboxes

The following code turns an image on or off based on whether there are two checkboxes checked. The problem is as I add checkboxes only the last two work correctly. In the example below the image does not turn on and off if chk_Pipe1N and chkIn1 are both checked. However it works perfectly when chk_Pipe2N and chkIn2 are both checked. If I add chk_Pipe3N and chkIn3 it will work for this set, but set 1 and 2 no longer work. Any ideas why?
'NIn
If Me.chk_Pipe1N Or Me.chk_Pipe2N And Me.chkIn1 Or Me.chkIn2 Then
Me.imgNIN.Visible = True
Else
Me.imgNIN.Visible = False
End If
'NOut
If Me.chk_Pipe1N Or Me.chk_Pipe2N And Me.chkOut1 Or Me.chkOut2 Then
Me.imgNOut.Visible = True
Else
Me.imgNOut.Visible = False
End If
Simpler (adding #HansUp's comment):
Me.imgNIN.Visible = (Me.chk_Pipe1N Or Me.chk_Pipe2N) And _
(Me.chkIn1 Or Me.chkIn2)

VB : Parentheses automatically added in error

My colleagues frown upon me because according to our sourcesafe, I added parentheses to the method-property thus resulting in a stackoverflow.
Since I'm sure I did no such thing on purpose, I wonder if somehow I used a obscure shortcut which performed these changes.
This kind of stuff :
Public Function Opslaan() As Boolean
If something_wrong() Then
opslaan = False
End If
If opslaan = False Then
Do_Something_Else()
End If
Return opslaan
End Function
and this got changed into :
Public Function Opslaan() As Boolean
If something_wrong() Then
opslaan = False
End If
If opslaan() = False Then '' See the parentheses here added automatically
Do_Something_Else()
end If
Return opslaan
End Function
Any help is appreciated.
This looks like bad old VB6 code converted to VB.NET.
The problem is VB6 allowed you to treat the current function name as a variable and update its value all throughout the method. Then, when you exited the method whatever value this variable had would be returned to the caller. This syntax is confusing and should never be used, even in VB6 since there is a better way.
Update all code you find like this to something like:
Public Function Opslaan() As Boolean
Dim result As Boolean = True
If something_wrong() Then
result = False
end if
If result = False Then
Do_Something_Else()
End If
Return result
End Function
This is much clearer code and will never mistakenly call the same routine.
Also, this is totally personal preference, but I prefer to not check booleans with equals statements. So If result = False Then becomes If Not result Then and If result = true Then becomes If result Then. That almost always feels cleaner to me for Booleans.

MS-Access hidding and showing a button conditionally through vba

I am developing a quality control system for my company, and I i want to connect it to a label printer, which is already taken care off, the problem now is with the button itself.
I want the print label button to only be enabled and visible after the whole check was made, what i've got now is this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Button_label.Visible = False
Button_label.Enabled = False
End Sub
Private Sub Motor_OK_Change()
Dim ok As Boolean
ok = Motor_OK.Value
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
End Sub
It does work on conceiling the button, but it fails when trying to enable it again and making it visible upon making the check. It's important to refer that I have tried using If ok = True instead of If ok= 1, I dont know how important that is.
Thanks
You are not testing for both conditions (ok=0), perhaps the following will work:
Replace:
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
With:
Button_label.Visible = ok
Button_label.Enabled = ok
New Solution:
Instead of using the Form Before_Update event, use Form_Current event. If there is only 1 record, you can also use the Form_Load event. I would also suggest that you remove the Visible property and use only the Enabled one so users can see their is print functionality.
Private Sub Form_Current()
'button_Label.Visible = False
button_Label.Enabled = False
End Sub
Instead of using the Motor_OK Before_Updateevent, use After_Update event
Private Sub Motor_OK_AfterUpdate()
Dim ok As Boolean ' defaults to False
If IsNumeric(Motor_OK.Value) Then ok = True ' remove this statement if Motor_OM is
' alpha numeric.
If Not IsNull(Motor_OK.Value) Then ok = True ' otherwise following statement
' to avoid NULL error
'button_Label.Visible = ok
button_Label.Enabled = ok
End Sub
Let me know if I can be of further help.

Multiple Combobox selection Acess VBA

I am trying to create a VBA code that says, if combobox equals this or this or this then have radio button populate. When i use only one bucket (134) it works fine but when i try ti add multiple sections, i get and err.
My code:
If Me.cmbBucketFilter = "134,135,136" Then
'Working Echo Triggers
Me.fmeReached.Visible = True
Me.fmeRecovered.Visible = True
Me.fmeError.Visible = False
Else
'Working Another Bucket
Me.fmeError.Visible = True
Me.fmeReached.Visible = False
Me.fmeRecovered.Visible = False
End If
You're asking it to run only if cmbBucketFilter equals "134, 135, 136" (which it never can). What you really want is for it to run if it equals any of those values.
Change the first line to this:
If Me.cmbBucketFilter IN ("134", "135", "136") Then