Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to spawn (a demon)?

Asked by 9 years ago

Not literally, I hope no admin bans me blindly for trolling. Anyways, I am really dumb with scripting and I want to spawn this demon every nighttime in my game for Fall/October. However it's not working. Does anyone know what I'm doing wrong/should I use a different method?

local time = game.Lighting.TimeOfDay

if time == "00:00:00" then
    demonic = game.Lighting.Demon:Clone()
    demonic.Parent = game.Workspace

elseif time == "03:33:33" then
    game.Workspace.Demon:Destroy()

end

Thanks for the help in advance, and Happy Halloween (You don't know how excited I am)

0
Is there any error that show up in the output? User#9949 0 — 9y

3 answers

Log in to vote
1
Answered by
Marios2 360 Moderation Voter
9 years ago

(For confusion avoidance, know that in this answer i refer to branflakes1099 in second person and to CarterTheHippo in third person.)

CarterTheHippo brought you a correct answer, i would like to give you the same but much more effecient.

So, my fellow contributor used an event. Events fire when crucial actions happen. Here are the events every instance has:

AncestryChanged (When path to instance changes) ,Changed (When any property of instance changes) ,ChildAdded (When instance gets a child) ,ChildRemoved (When instance loses a child) ,DescendantAdded (When instance itself or any instance below the parent instance gets a child) DescendantRemoving (When instance itself or any instance below the parent instance loses a child)

To know when those crucial actions happen you set up a listener for the event, like here:

game.Lighting.Changed:connect(function(property)

For we want to do, we are using the Changed event here. This event will fire when any property of the Lighting service (in this case) changes. Notice "property" in the function - most (but probably all) events return values for functions to use. In this case we have a string of the property's name, which i chose to refer to as "property" in the function.

However, hold it! What about what Carter did?

game.Workspace.TimeOfDay.Changed(function(val)

This event looks the same but is different. Roblox provides instances which can be used to hold specific values, in which case it's StringValue here. When you use the Changed event on any value container, the event will always fire when the value changes, and never otherwise. Thus for this we never need to check if it's the value that's changed.

As i said before, his solution is correct but a bit ineffecient. What we can do with the Changed event will allow us to not require a StringValue to keep the property and an additional script to update it.

So, on our script here, for checking a specific property (in your case TimeOfDay), we can just use an ifstatement to see if the name of the property is TimeOfDay.

This is the final product:

game.Lighting.Changed:connect(function(property)
    if property == "TimeOfDay" then --If the time of day changed then...
        if val == "00:00:00" then
            game.Lighting.Demon:Clone().Parent = game.Workspace
        elseif val == "03:33:33" then
            game.Workspace.Demon:Destroy()
        end
    end
end) --We always use a parenthesis on here to end the event function

Hopefully this answer helps you understand Lua further.

0
I'm using a remote event for another one of my scripts, Im having trouble getting it to work, maybe you could take a look at it? CarterTheHippo 120 — 9y
0
Thank you very much! branflakes1099 30 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Well I have an easy way to do it.

Step 1: Insert A StringValue into the Workspace (If you do not know how to do this just, Go to the models tab, click on it, and look for AdvancedObjects. Then click on it. It should pop up somewhere on your screen. Click on workspace then scroll through the advanced objects until you find "StringValue") Double click it to insert.

Step 2: Name the String Value "TimeOfDay"

Step 3: Insert a regular script into the String value so that the Script's Parent is the StringValue.

This is the script that goes inside the StringValue:

wait()
while true do
    wait()
    script.Parent.Value = game.Lighting.TimeOfDay
end

This script will make it so that the Value of The StringValue will be the exact TimeOfDay

For Example: if it is 00:00:00, the Value in the string Value will be "00:00:00"

Step 4: Insert a regular script into Workspace

This will be the script that goes into workspace

game.Workspace.TimeOfDay.Changed:connect(function(val) 
    if val == "00:00:00" then
        game.Lighting.Denom:Clone().Parent = game.Workspace
    elseif val == "03:33:33" then
        game.Workspace.Denom:Destroy()                                  
    end
end)

What this script does is that if the time of day changes at any moment it will check to see if the time of day is "00:00:00" or "03:33:33"And if it happens to be one of those times, it will either Clone or Destroy the Demon.

Note: You have to use the string Value because you can't do an OnChangedEvent with the Lighting.TimeOfDay

I have tested this script and it works for me! Good luck on your game!

Log in to vote
0
Answered by 9 years ago

Actually, I lied. This isn't working for either solutions. Maybe is it because the Demon is a model? Not sure.

Answer this question