generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: trying to make script that loops and checks if value = true

Previous Thread :: Next Thread 
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 11:29 AM
im not sure how to exactly do it. any help please?


while true do do
--objective 1
if script.Parent.Value == true then

---objective stuff here

print("Objective 1 active")

end

end
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 11:30 AM
the second do in "while true do" has been removed, but it still isnt working
Report Abuse
LaeMVP is online. LaeMVP
Joined: 24 Jun 2013
Total Posts: 4416
30 Jul 2017 11:30 AM
while condition do
code
end

while(condition){
code
}

Which one will work for you? :thinking:
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 11:32 AM
If you need an infinite loop, you need to add a wait() statement somewhere so you give the rest of the game time to run (the engine can only do one thing at a time, and will run a script forever until it yields or finishes execution)
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 11:36 AM
Other than that, are you getting any error messages in the output window? Other than that second do (which you fixed), I don't see anything syntactically wrong with your code, though I could just be blind.
Report Abuse
systematicaddict is not online. systematicaddict
Joined: 13 Jul 2012
Total Posts: 4188
30 Jul 2017 11:36 AM
@Lae isn't that C#? I don't think you can even do that
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 11:42 AM
That's how most C-style languages format code, not just C#. Doesnt work like that in Lua, but would work if you just got rid of the brackets and added a do/end. You don't need the parentheses in Lua either, but you can use them for if and while if you want since they don't change anything about the conditional (they don't work for for loops, though)
Report Abuse
PuddyTats is not online. PuddyTats
Joined: 03 Apr 2008
Total Posts: 79351
30 Jul 2017 11:42 AM
The second one is a lot of languages, actually.
C, C++, Java, Javascript, etc all do it that way


BUT I'M HERE TO SAY WHILE LOOPS ARE BAD
If you're checking to see if a value is true, you can check .Changed and react to the value being set to true.


-


-
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 11:50 AM
i forgot about .changed. How do I use it? It's probably alot better than what im trying to do... lol
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 11:54 AM
[object here].Changed:connect(
function(property)

-- CODE
end)


The property variable passed in gives the string name of the property that was changed. You can ignore it if you don't need it, but could be useful.
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 11:58 AM
i want it to specifically check if the value turns true though. i will also have more lines of code to check if the value turns false for specific reasons.
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 12:07 PM
( put this in the -- CODE part of what I wrote above)

if property == "{name of property you want to check}" then
if part.{property you want to check} == true then
-- changed to true
else
-- changed to false
end
end
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 12:18 PM
im not exactly sure what to do so ill just post this here with some comments:


--NOTE" script.Parent.Value = the BoolValue (it only has true and false)

script.Parent.Value.Changed:connect(
function(property)
if property == "{name of property you want to check}" then

-- ^ not exactly sure what the name of property is; is it script.Parent.Value? not sure

if script.Parent.Value == true then
print("Objective 1 active")
else
print("Objective 1 inactive")
end
end

end)
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 12:22 PM
The name of the property is "Value", and you want to do this:

script.Parent.Changed:connect(...

Not

script.Parent.Value.Changed::connect(...
Report Abuse
A_Ferret is not online. A_Ferret
Joined: 15 Feb 2016
Total Posts: 8432
30 Jul 2017 12:25 PM
Put wait()
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 12:26 PM
tried it. still, nothing happens. nothing is disabled or anything. nothing pops in console (even the print)

script.Parent.Changed:connect(
function(property)
if property == "Value" then
if script.Parent.Value == true then
print("Objective 1 active")
else
print("Objective 1 inactive")
end
end

end)


(this is the whole script btw)
Report Abuse
spacesplitter is not online. spacesplitter
Joined: 10 Jul 2015
Total Posts: 44
30 Jul 2017 12:28 PM
while wait() do
if script.Parent.Value==true then
-code
print("Objective 1 Active")
end
end
Report Abuse
spacesplitter is not online. spacesplitter
Joined: 10 Jul 2015
Total Posts: 44
30 Jul 2017 12:30 PM
There is another way to do it
script.Parent.Changed:Connect(function()
if script.Parent.Value==true then
--Objective stuff here
print("Objective 1 Active")
end
end
Report Abuse
MICHEAL1988351 is online. MICHEAL1988351
Joined: 26 Jan 2015
Total Posts: 582
30 Jul 2017 12:33 PM
you have an extra 'do' block, and no end to it; therefore you would get an error and your thread would halt execution if your studio didn't already freeze.
To check if a value has changed simply by repetitive iteration in roblox studio you can use a while loop:

local wte = wait;
local pnt = print;
while wte() do
pnt((obj.Value == true and "obj is equal to true!") or "obj has not been changed");
end

In Lua when creating an iteration that will run for an indefinite amount of time, it is always smart to create local variables of globals that you would constantly be calling inside of the iteration block, since locals are accessed faster than globals. In the long run the above while iteration will run exponentially faster than a while iteration that repetitively uses globals.
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 12:35 PM
none of these seem to be working still. if it helps, the script is located in a boolvalue, which is located in a folder, which is located in ReplicatedStorage.
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
30 Jul 2017 12:38 PM
Just tested with BoolValue, it seems like the Changed event works differently than on most other objects.

With most objects (like parts) it passes in the name of the property chat is changed, however, with Value objects (IntValue, BoolValue, etc) it

A) only fires if the value changes (not the name, parent, or any other property)
B) passes in what the value was changed to.

This is what it looks like in my script I was using to test this:

local onChanged = script.Parent.Changed:connect(function(property)
if property == true then
print("True")
else
print("False")
end
end)


Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 12:50 PM
i found out something weird: script.Parent.Changed:connect(function(NewValue) if NewValue == true then print("Objective 1 IS ACTIVE!!!") end if NewValue == false then print("Objective 1 isnt active #### end end) this script works in Workspace and ServerScriptService. This means all this time those other scripts worked, but they just didnt work in ReplciatedStorage or ServerStorage. I dont know why, but it doesnt really matter because I will likely be moving my scripts to ServerScriptService now,
Report Abuse
Sk8erDan is not online. Sk8erDan
Joined: 22 Mar 2010
Total Posts: 4565
30 Jul 2017 12:51 PM
thanks ROBLOX for literally censoring a sad face...
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image