Attempt to modify readonly table error happening where it defines function

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make functionality that uses os.clock() to determine when a certain month is in effect so I can try to “replicate” seasons in Ocean’s World.

  2. What is the issue? Include screenshots / videos if possible!
    When I try to run the script, it errors on line 2, where a function is being defined, meanwhile…
    Attempt to modify a readonly table - Line 2

here’s the code:

-- Custom string split function
function split_string(textstring, sep)
	local fields = {}
	local pattern = string.format("([^%s]+)", sep)
	textstring:gsub(pattern, function(c) fields[#fields + 1] = c end)
	return fields
end

-- OS Clock
local os_clock = os.date("%Y-%m-%d")
local os_splits = split_string(os_clock, "-")

-- Year, Month and Day
local year = os_splits[1]
local month = os_splits[2]
local day = os_splits[3]
  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I’ve tried looking for solutions to the error, but apparently I’ve found nothing, and I can’t find any solution to this, so my last hope was here.

Please help :slight_smile:
I hope u’ve drank ur water!

1 Like

I’ve tested your code, and I can’t seem to get the same issue:
image

Tested in both a local script and a server script.

Are you 100% sure that that is the code that is causing the error?

I don’t run into this error. Are you sure there isn’t any other information that should be relevant?

Either way, you shouldn’t use this method. First, os.date returns a table if given the argument "*t" or "!*t". You should use that, as you can then simply index keys year, month, and day directly.

If you had checked the API documentation, you’d know that Roblox recommends using DateTime instead. In which case, you’d use DateTime.now to get the current time, which would have a similarly structured table (with Year, Month, Day).

You really should check the documentation next time.

Wouldn’t that just give me the same thing as os.date? And also, it doesn’t return a table if I add “%Y-%m-%d” as the parameter for it.