Friday, June 14, 2013

The Changed Function

Changed is a function used often around ROBLOX, many users consider it a treasure to use in their game as it's brilliant at firing certain options once an element/property of an item with the changed event in it has been changed.
Example:
game.Lighting.Changed:connect(function(change) --This calls the function, names the event, stores the name of the changed property into the variable 'change'. 
if change == "TimeOfDay" then --Makes sure the changed item of lighting is the Time before it does anything.
 for _,parts in pairs (game.Workspace:GetChildren()) do --This gets the parts in the workspace, stores them into a variable named 'parts'. 
if parts:IsA("BasePart") then --This checks if the parts superclass/class is a BasePart/Part. 
for _,v in pairs (parts:GetChildren()) do --This gets the children of parts and stores them into the variable v. 
if v:IsA("Light") then --This checks if the children of parts is a light. 
if game.Lighting.TimeOfDay == "17:00:00" then -- This checks if the time of day is 17:00:00 (in-game time)
v.Enabled = true -- This enables the lights 
elseif game.Lighting.TimeOfDay == "08:00:00" then --This checks if the time is 8am (in-game) 
v.Enabled = false -- This enables if above is true. 
 end end end end end end end) -- This ends the code. 


You can use the changed function to do many things, it is only fired when something in the class you are using it in has changed. Therefore it's easier than using any while loop to keep checking with an if statement.

Fiddle around with the above code, learn from it.

This was a short tutorial/example by Zakary Wilson.
Thanks for reading.

No comments:

Post a Comment

Write your comment here... OR Flingi will eat you