My script is always giving the value 6 even tho its not Sunday or saturday

My script is always giving Value 6 when its not Sunday or saturday
Heres the script below

local RSK = math.random(1,6)
local day = os.date("!*t").wday

if day == 1 or 7 and RSK ~= 6 then
    RSK = 6
end
-- for some reason it output 6 even when its not sunday or saturday

anyone can give me a help with this script?

does the game start at any day besides sunday and saturday
what is your code trying to do

Yes, my game start at any day besides sunday and saturday (today is wednesday in UTC)

The RSK Value will be converted to text heres the full script

local Val = game.ReplicatedStorage
local RSK = math.random(1,6)
local day = os.date("!*t").wday

if day == 1 or 7 and RSK ~= 6 then
	RSK = 6
end


wait(1)
if RSK == 1 then
	Val.Risk.Value = "TSTM"
elseif RSK == 2 then
	Val.Risk.Value = "MRGL"
elseif RSK == 3 then
	Val.Risk.Value = "SLGT"
elseif RSK == 4 then
	Val.Risk.Value = "ENH"
elseif RSK == 5 then
	Val.Risk.Value = "MDT"
elseif RSK == 6 then
	Val.Risk.Value = "HIGH"
end

if day == 1 or day == 7 then , Never use if day == 1 or 7 then

the “or” between 1 and 7 doesnt help at all, it will cause ur IF statement to always be true

same example with

while true do

end

true is always true, just like 7 is always 7, thats why u must say “or day == 7” not “or 7”

use this instead

local Val = game.ReplicatedStorage -- locate ur instance
local RSK = math.random(1,6)
local day = os.date("!*t").wday

if day == 1 or day == 7 and RSK ~= 6 then
	RSK = 6
end

local MyTable = {
	"TSTM",
	"MRGL",
	"SLGT",
	"ENH",
	"MDT",
	"HIGH",
}

task.wait(1)

Val.Risk.Value = MyTable[RSK]

This is a classic mistake. Let’s go over truthy and falsy values really quick.

We know the standard:

if true then
    -- runs always
end

Of course there are logical operations we can use, and, or, and not:

if true and true then
    -- runs always
end
if not true or false then
    -- runs never
end

This is great, but you might have seen something this:

local x = workspace:FindFirstChild("x")

if not x then
    return
end

Or this:

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

Or even this:

local text = name and name or "No Name"

This is because Luau has truthy and falsy values, i.e. values that act as if true or as if false when used in a boolean context. All values in Luau are truthy except false and nil, so when we look back at our examples they might make a bit more sense with this info:

local x = workspace:FindFirstChild("x") --> `Part?` (`Part | nil`)

--[[
    `x` cannot be true or false, it can be `Part` or `nil`,
    in this case, if it is `nil` (falsy) then `not x` will result
    in `true` (`not falsy` is defined to be `true`)
]]
if not x then
    -- we know here that `x` is `nil`
    return
end

-- we know here that `x` is `Part`
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
-- we need a character here, but the character might not be there just yet.
--[[
    in this case it is popular to use the below pattern which uses the truthiness
    or falsiness of `localPlayer.Character` to *select* a value. it does not evaluate
    to a boolean `true` or `false`. this also shows "short-circuiting" behaviour, in which
    the expression completes evaluating when the result is guaranteed, for example
    in `true or somethingElse` the expression can immediately evaluate to `true` by
    virtue of `or` always being `true` if either of its operands are `true`, so `somethingElse`
    is never evaluated, if it were a function call for example that function would never
    be called. the same is the case for `false and somethingElse`, we know that `and` should
    be `false` if either of its operands are `false`, so `somethingElse` need not be evaluated.
]]
local character =
    localPlayer.Character or --> `Model?`; if truthy the expression evaluates to this
    localPlayer.CharacterAdded:Wait() --> `Model`, evaluates if and only if falsy
--[[ 
    we can see short-circuiting behaviour used in the form of a "ternary operator" 
    which selects between between 2 values based on a condition
]]
local text = 
    name and  --> if `name` is falsy (`nil`) short circuits to `false` for the fallback case
    name or   --> if `name` is truthy (`string`) evaluates to `name`
    "No Name" --> if `name` was falsy becomes `false or "No Name"` which evaluates to `"No Name"`

Note for the last example Luau has a built-in ternary operator called an if-then-else expression which is much more readable:

local text = if name then name else "No Name"

Note that if-then-else expressions can only have single expressions in their arms.

So in your case, day == 1 or 7 you either get true or 7 or false or 7, which will always result in the truthy value 7, in which case you get true and RSK ~= 6, which is the same as RSK ~= 6. The proper way to write what you intend to express is day == 1 or day == 7 and RSK ~= 6.

The problem is rather simple, you put in your code:
if day == 1 or 7, what this does is tell if day is == 1, or if 7 is something that exists, thus it always returns true, try this

local RSK = math.random(1,6)
local day = os.date("!*t").wday

if (day == 1 or day == 7) and RSK ~= 6 then
    RSK = 6
end
-- for some reason it output 6 even when its not sunday or saturday

The parens aren’t even needed here, it works fine without them.

Thats a good point, mb lol

Wait actually no we would need them, cause from my understanding if day is equal to 1 or 7, and RSK is equal to 6, then we wouldnt want to set it, without the parentheses it would act a little silly

i think so too, cuz it will be like

if day == 1 then

end
--Or
if day == 7 and RSK ~= 6 then

end

but eitherway, the “and RSK ~= 6” isnt even that needed here, updating the RSK into something it already is (like 6) wont cause any harm

1 Like

Ty for everyone who gived me a fix and a tip and a improvement on my script I hope everyone has a good day!

1 Like