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

OOP methods aren't refrencing the same values?

Asked by 4 years ago

I'm trying to make a simple time module that fires events when a specific time is reached (sunrise, sunset, midnight) allows me to control the time of day and the speed. For some reason my scripts aren't refrencing values inside of the self table. I changed the toggle method to set self.time and self.multiplier within itself and it worked fine, but when i try doing it by calling another method, it doesn't change the values the rest of the method refrences. It does change A self.time or self.multiplier value, but not the same value self.time or self.multiplier value the rest of the script refrences. Same goes with self:update(), except even when i change self.enabled to true within TOD.new it still doesn't activate and instead prints "it aint enabled" for some reason. Please help.

module script

local TOD = {}
local TOD_mt = {__index = TOD}


function TOD.new()
    local self = {}

    self.time = lighting:GetMinutesAfterMidnight()
    self.multiplier = 1 -- how many irl seconds is 1 in game minute?
    self.enabled = false

    return setmetatable(self,TOD_mt)

end


function TOD:ChangeSpeed(multiplier)
    self.multiplier = multiplier
end

function TOD:ChangeTime(gametime)
    self.time = gametime
end


function TOD:Toggle(toggle,gametime,multiplier)
    self.enabled = toggle

    if (toggle and multiplier) then
        TOD:ChangeSpeed(multiplier)
    end
    if (toggle and gametime) then
        TOD:ChangeTime(gametime)
    end
    if toggle then
        TOD:update()
    end
end

function TOD:update()
    if not self.enabled then
        print("it aint enabled")
    end
    while self.enabled do
        print("time is changing! Time: "..self.time)
        lighting:SetMinutesAfterMidnight(self.time + 1)
        self.time = lighting:GetMinutesAfterMidnight()
        wait(self.multiplier)
    end
end


return TOD

server script

TOD = require(script.Parent.Time).new()

wait(3)
print("changing time")
TOD:Toggle(true,5*60 + 45,0.001)
print("changing")
wait(3)

print("changing speed")
TOD:ChangeSpeed(1.5)

Answer this question