#An if statement inside my script does not work no matter what I try

1 messages · Page 1 of 1 (latest)

quartz moth
#

Here is my script I will also send a video too of what I mean

Im more concerned about this part of my code


while true do--BUG doesnt work if I switch the clocktime manually via Properties Panel or via DayNightCycle Script
    task.wait(2.016)

    if (Lighting.ClockTime >= 18 or Lighting.ClockTime <= 6) and alreadyplayed == false then
        warn("Switching Lamp WORKS")
        SwitchLamp()
    end
end
#

local Lighting = game:GetService("Lighting")

local SoundEnded = false

local LeaderBoardLamp = workspace.Light:WaitForChild("LeaderBoardLamp")
local alreadyplayed = false

local LampTurnONorOFFSound = workspace.Light.LeaderBoardLamp:WaitForChild("LightTurnONorOFF")
local LampBuzzingSound = workspace.Light.LeaderBoardLamp:WaitForChild("LightBuzzing")
local LampFlickeringSound = workspace.Light.LeaderBoardLamp:WaitForChild("LightFlicker")
local LampFlickeringSound2 = workspace.Light.LeaderBoardLamp:WaitForChild("LightFlicker2")




local function FlickerLoop()
    while true do
        LampFlickeringSound2:Play()

        LeaderBoardLamp.Material = Enum.Material.Plastic--------------Flickering
        LeaderBoardLamp.SpotLight.Brightness = 0
        task.wait(0.1)

        LeaderBoardLamp.Material = Enum.Material.Neon
        LeaderBoardLamp.SpotLight.Brightness = 20.18
        task.wait(0.1)

        LeaderBoardLamp.Material = Enum.Material.Plastic
        LeaderBoardLamp.SpotLight.Brightness = 0
        task.wait(0.1)

        LeaderBoardLamp.Material = Enum.Material.Neon
        LeaderBoardLamp.SpotLight.Brightness = 20.18---------------

        LampBuzzingSound.TimePosition = 0

        LampBuzzingSound:Play()

        LampFlickeringSound2.TimePosition = 0

        LampBuzzingSound.Ended:Wait()


        if Lighting.ClockTime > 6 and Lighting.ClockTime < 18 then--Breaks if the Loop is under 18 clocktime or over 6 clocktime
            LeaderBoardLamp.SpotLight.Brightness = 0
            LampBuzzingSound:Stop()
            LampFlickeringSound:Stop()
            LeaderBoardLamp.Material = Enum.Material.Plastic
            alreadyplayed = false
            LampTurnONorOFFSound:Play()
            break
        end

    end
end
#
local function SwitchLamp()
    alreadyplayed = true
    LampFlickeringSound:Play()
    task.wait(1.406)

    LeaderBoardLamp.Material = Enum.Material.Neon
    LeaderBoardLamp.SpotLight.Brightness = 20.18

    LampTurnONorOFFSound:Play()
    task.wait(2.016)
    LampTurnONorOFFSound.TimePosition = 0

    LampBuzzingSound:Play()
    LampBuzzingSound.Ended:Wait()
    LampBuzzingSound.TimePosition = 0 

    task.spawn(FlickerLoop)
end

while true do--BUG doesnt work if I switch the clocktime manually via Properties Panel or via DayNightCycle Script
    task.wait(2.016)

    if (Lighting.ClockTime >= 18 or Lighting.ClockTime <= 6) and alreadyplayed == false then
        warn("Switching Lamp WORKS")
        SwitchLamp()
    end
end

#

this is all in one script btw. It didnt fit in one message though

timid light
#

whats the issue with it

#

@quartz moth

quartz moth
#

here @timid light

timid light
quartz moth
#

im adding a video

timid light
#

whats the issue with it

#

oh ok

quartz moth
timid light
#

im not watching 4 minutes of a video

quartz moth
#

i can explain u rq

#

@timid light

timid light
#

yea just describe it

quartz moth
#

so I have this Day and Night cycle script that I am trynna implement with this script right

timid light
#

yea

quartz moth
#

This lamp script does not run if I change the Lighting,Clocktime property via the Property tab or this Day and Night Cycle script in game

#

only if I set the clock time to 18 before I run the game it actuallly runs which is very weird

timid light
#

ok i think i see something thats potentially an issue

#

you're setting AlreadyPlayed to true in the SwitchLamp function without a return

#

are you intending to change that variable across the whole script?

quartz moth
#

yes

timid light
#

its not gonna change it without a return

quartz moth
#

where should I put the return

timid light
#

its a little weird but i typically just leave a return on every function i write

#

careful though, return will end the function instantly regardless of where it is so put it at the end

#

example:
function myfunction()
print("G")
return
print("H")
end

#

calling myfunction here will only print G

quartz moth
#

oh

icy condorBOT
#

studio** You are now Level 4! **studio

timid light
#

moving the return to the end of the function will print both G and H

#

i have NO CLUE if this is the issue but this might fix it

quartz moth
#

ill try it tho

#

thx

timid light
#

try to put these on all the functions

quartz moth
#

yeah okay

timid light
#

did that work

quartz moth
#

nope

#

So ive tested something

#

its not about the boolean itself

#

its about the clocktime

#

i think

timid light
#

also is this the whole script?

quartz moth
#

yes

#

It didnt fit in one message

timid light
#

ah

#

i just dont see why you made a function for FlickerLoop

#

are you planning on calling it more?

#

otherwise just put it in SwitchLamp

quartz moth
#

yeah i just made that function cuz if that buzzing sound effect stopped that it would make a tiny flicker and then loop back around

#

ive noticed you can just loop it via properties though

#

I just dont get this bug though

timid light
#

i dunno

#

what line is supposed to be turning on the lamp?

quartz moth
#

im gonna fix this tomorrow ig im way too tired

timid light
#

alr

#

you might wanna rewrite this, gotta be honest this is written really weirdly

#

plan out how the code will be handled before you write it

quartz moth
#

you dont wanna know what I wrote the first time

#

i will rewrite it then

#

cya

quartz moth
#

omfg im so stupid

#

I fixed it

#

@timid light