Daft: the only module script you ever need

daft is the most useful module script

local daft = {}
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local Http = game:GetService("HttpService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local heart = game:GetService("Stats")

local loopprevention = 0

local cache = 0
local cache2 = 0
local cache3 = 0
local cache4 = 0
local cache5 = 0
local cache6 = 0
daft.cache7 = 0 -- Emerald rng

--[[
use for limiting a number to a range, if you want to squish down lets just say a random value that ranges 1 to 100 to 5 then you would do
daft:limit(math.random(1,100), 5, 1)
min defaults to 1
]]

function daft:Shuffle(tbl)
	local returntbl={}
	if tbl[1]~=nil then
		for i=1,#tbl do
			table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
		end
	end
	return returntbl
end

function daft:limit(x:number, max:number, min:number)
	min = min or 1
	if not max then
		error("NO MAX PROVIDED")
	end
	if max <= min then
		error("max lower than or equal to min")
	end
	-- Wrap x around the range [min, max]
	local rangeSize = max - min + 1
	local wrapped = ((x - min) % rangeSize) + min
	return wrapped
end
--[[ 
check if any of table 1 is in table 2
]]
function daft:tablechecker(tab, othertable)
	for i,v in tab do
		if table.find(othertable, v) then			
			return true
		end
	end
	return false
end
--[[ returns the first matching element in table ]]
function daft:tablefinder(tab, othertable)
	for i,v in tab do
		if table.find(othertable, v) then			
			return v
		end
	end
	return nil
end
--[[ those who check ]]
function daft:CheckIfPlayerIsInArea(Part:BasePart,Character:Model)
	if not Character:FindFirstChild("HumanoidRootPart") then
		return false
	end
	local touching = workspace:GetPartsInPart(Character.HumanoidRootPart)
	for i=1,#touching do
		if touching[i] == Part then
			return true
		end
	end
	return false
end
--[[check if a player is in the game]]
function daft:playerchecker(userid:number)
	for i, v in Players:GetPlayers() do
		if v.UserId == userid then
			return true
		end
	end
	return false
end
--[[
use in place of that old boiler code, yada its obvious where you put the badge value
]]
function daft:badgelooper(badge: number)
	for i, v in game:GetService("Players"):GetPlayers() do
		if game:GetService("BadgeService"):UserHasBadgeAsync(v.UserId, badge) == false then
			game:GetService("BadgeService"):AwardBadge(v.UserId, badge)
		end
	end
end
--[[
converts seconds into weeks days hours minutes seconds
]]
function daft:secondstotime(seconds:number)
	local weeks = seconds//604800
	local remainingSeconds = seconds % 604800
	local days = remainingSeconds//86400
	remainingSeconds = remainingSeconds % 86400
	local hours = remainingSeconds//3600
	remainingSeconds = remainingSeconds % 3600
	local minutes = remainingSeconds//60
	remainingSeconds = remainingSeconds % 60
	local secs = remainingSeconds

	local timeUnits = {
		{value = weeks, label = " Weeks "},
		{value = days, label = " Days "},
		{value = hours, label = " Hours "},
		{value = minutes, label = " Minutes "},
		{value = secs, label = " Seconds "},
	}

	local completedstring = ""
	for i, unit in timeUnits do
		if unit.value > 0 then
			completedstring = completedstring .. unit.value .. unit.label
		end
	end

	if completedstring == "" then
		completedstring = "0 Seconds"
	end

	return completedstring
end
--[[
converts seconds into eveyears evemonths weeks days hours minutes seconds
]]
function daft:secondstoevetime(seconds:number)
	-- evemonths are 30 days, eveyears are 360 evedays, evedecades are 10 eveyears, evecenturies are 10 evedecades use this when dealing with alot of seconds
	local evecenturies = seconds//3110400000
	local remainingSeconds = seconds % 3110400000
	local evedecades = remainingSeconds//311040000
	remainingSeconds = remainingSeconds % 311040000
	local eveyears = remainingSeconds//31104000
	remainingSeconds = remainingSeconds % 31104000
	local evemonths = remainingSeconds//2592000
	remainingSeconds = remainingSeconds % 2592000
	local weeks = remainingSeconds//604800
	remainingSeconds = remainingSeconds % 604800
	local days = remainingSeconds//86400
	remainingSeconds = remainingSeconds % 86400
	local hours = remainingSeconds//3600
	remainingSeconds = remainingSeconds % 3600
	local minutes = remainingSeconds//60
	remainingSeconds = remainingSeconds % 60
	local secs = remainingSeconds

	local timeUnits = {
		{value = evecenturies, label = " Evecenturies "},
		{value = evedecades, label = " Evedecades "},
		{value = eveyears, label = " Eveyears "},
		{value = evemonths, label = " Evemonths "},
		{value = weeks, label = " Weeks "},
		{value = days, label = " Days "},
		{value = hours, label = " Hours "},
		{value = minutes, label = " Minutes "},
		{value = secs, label = " Seconds "},
	}

	local completedstring = ""
	for i, unit in timeUnits do
		if unit.value > 0 then
			completedstring = completedstring .. unit.value .. unit.label
		end
	end

	if completedstring == "" then
		completedstring = "0 Seconds"
	end

	return completedstring
end
--[[
converts seconds to hexweeks hexdays hexhours hexminutes hexseconds
]]
function daft:secondstohextime(seconds:number)
	-- each hex unit is base 6 100 times longer then the last, or base 10 36 times longer then the last
	local evecenturies = seconds//503884800
	local remainingSeconds = seconds % 503884800
	local evedecades = remainingSeconds//83980800
	remainingSeconds = remainingSeconds % 83980800
	local eveyears = remainingSeconds//13996800
	remainingSeconds = remainingSeconds % 13996800
	local evemonths = remainingSeconds//1399680
	remainingSeconds = remainingSeconds % 1399680
	local weeks = remainingSeconds//279936
	remainingSeconds = remainingSeconds % 279936
	local days = remainingSeconds//46656
	remainingSeconds = remainingSeconds % 46656
	local hours = remainingSeconds//1296
	remainingSeconds = remainingSeconds % 1296
	local minutes = remainingSeconds//36
	remainingSeconds = remainingSeconds % 36
	local secs = remainingSeconds

	local timeUnits = {
		{value = evecenturies, label = " Hexcenturies "},
		{value = evedecades, label = " Hexdecades "},
		{value = eveyears, label = " Hexyears "},
		{value = evemonths, label = " Hexmonths "},
		{value = weeks, label = " Hexweeks "},
		{value = days, label = " Hexdays "},
		{value = hours, label = " Hexhours "},
		{value = minutes, label = " Hexminutes "},
		{value = secs, label = " Hexseconds "},
	}

	local completedstring = ""
	for i, unit in timeUnits do
		if unit.value > 0 then
			completedstring = completedstring .. unit.value .. unit.label
		end
	end

	if completedstring == "" then
		completedstring = "0 Hexseconds"
	end

	return completedstring
end
-- for when you get the stupid iso 8601 date and you want the unix stamp
function daft:parse_json_date(json_date)
	local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-])(%d?%d?)%:?(%d?%d?)"
	local year, month, day, hour, minute, 
	seconds, offsetsign, offsethour, offsetmin = json_date:match(pattern)
	local timestamp = os.time{year = year, month = month, 
		day = day, hour = hour, min = minute, sec = seconds}
	local offset = 0
	if offsetsign ~= 'Z' then
		offset = tonumber(offsethour) * 60 + tonumber(offsetmin)
		if offsetsign == "-" then offset = offset * -1 end
	end

	return timestamp + offset
end
-- self explainatory
function daft:RemoveTableDupes(tab)
	local hash = {}
	local res = {}
	for _,v in ipairs(tab) do
		if (not hash[v]) then
			res[#res+1] = v
			hash[v] = true
		end
	end
	return res
end
--[[ those who know! ]]
function daft:Vector3RadiansToDegrees(vector: Vector3): Vector3
	return Vector3.new(
		math.deg(vector.X),
		math.deg(vector.Y),
		math.deg(vector.Z)
	)
end
-- ray man
function daft:RayToPart(ray)
	--this function takes the ray as a parameter. Below, it gets the midpoint.
	local MidPoint = ray.Origin + ray.Direction/2

	--below it creates the part and returns it. I'm sure you're familiar with properties
	local Part = Instance.new("Part")
	Part.Anchored = true
	Part.CanCollide = false
	Part.CFrame = CFrame.lookAt(MidPoint, ray.Origin)
	Part.Size = Vector3.new(1, 1, ray.Direction.Magnitude)
	Part.Parent = workspace

	return Part
end
-- below are some pseduorandom number generators, meant for fun and not for serious use as the results they give tend to be rather static
--[[
one of the more dynamic of the false nummber generators, name comes from the fact it uses all of the possible stats number to compile a final result, they change pretty often
]]
function daft:statsrand(min:number, max:number, floor:boolean)
	local finalresult = 0
	finalresult += heart.DataSendKbps
	finalresult += heart.DataReceiveKbps
	finalresult += heart.ContactsCount
	finalresult += heart.FrameTime
	finalresult += heart.HeartbeatTime
	finalresult += heart.MovingPrimitivesCount
	finalresult += heart.PhysicsReceiveKbps
	finalresult += heart.PhysicsSendKbps
	finalresult += heart.PhysicsStepTime
	finalresult += heart.PrimitivesCount
	finalresult += heart.RenderCPUFrameTime
	finalresult += heart.RenderGPUFrameTime
	if floor then
		finalresult = math.floor(finalresult)
		return daft:limit(finalresult, max, min)
	else
		return daft:limit(finalresult, max, min)
	end
end
--[[
its like he's in some king of... time... probably the most random out of the bunch
]]
function daft:timerand(min:number, max:number)
	return daft:limit(DateTime.now().UnixTimestampMillis, max, min)
end
--[[
like idrand, but for players currently in the game, more dynamic but not by much
]]
function daft:currentidrand(min:number, max:number)
	local finalresult = 0
	for i, v in game:GetService("Players"):GetPlayers() do
		finalresult += v.UserId
	end
	return daft:limit(finalresult, max, min)
end

--[[ picks a random random function ]]
function daft:randrand(min:number, max:number, dilute:boolean)
	if min == max then
		return max
	end
	min = math.floor(min)
	max = math.floor(max)
	local possilbities = {}
	-- uses a REALLY messy way of making things more rare, but it works
	table.insert(possilbities, daft:timerand(min, max))
	task.wait()
	table.insert(possilbities, daft:timerand(min, max))
	table.insert(possilbities, daft:idrand(min, max))
	table.insert(possilbities, daft:statsrand(min, max))
	task.wait()
	table.insert(possilbities, daft:statsrand(min, max))
	table.insert(possilbities, daft:ao3rand(min, max))
	table.insert(possilbities, daft:currentidrand(min, max))
	table.insert(possilbities, daft:chatrand(min, max))
	table.insert(possilbities, daft:soundrand(min, max))
	table.insert(possilbities, daft:heximalrand(min, max))
	table.insert(possilbities, daft:binaryrand(min, max))
	table.insert(possilbities, daft:otherstatrand(min, max))
	table.insert(possilbities, daft:EmeraldRNG(min, max))
	if dilute then -- dilutes the quasi-random with the almost-random
		for i = 1, #possilbities do
			table.insert(possilbities, math.random(min, max))
		end
	end
	local finalresult
	if math.random(1, 100) ~= 1 then
		loopprevention = 0
		finalresult = math.floor(possilbities[math.random(#possilbities)])
	elseif loopprevention > 10 then
		loopprevention = 0
		finalresult = math.floor(possilbities[math.random(#possilbities)])
	else
		loopprevention += 1
		finalresult = math.floor(possilbities[daft:randrand(1, #possilbities)])
	end
	if math.random(3) == 1 then -- to allow only SOME bias to creep through, this won't run everytime
		local loopcollider = true
		while loopcollider do
			if math.random(1, 100) <= 80 and loopprevention <= 100 then
				loopprevention += 1 -- math should be really quick, but because programmers must face the terror of worst case scenario, we have a loopprevention over here too
				finalresult = finalresult + math.floor(possilbities[math.random(#possilbities)])
			else
				loopcollider = nil
			end
		end
	end
	if finalresult > max then
		finalresult = daft:limit(finalresult, max, min)
	end
	return finalresult
end

--[[ sounds... ]]
function daft:soundrand(min:number, max:number)
	local result = 0
	for i, v in game:GetDescendants() do
		if v:IsA("Sound") then
			result += v.TimeLength
		end
	end
	return daft:limit(result, max, min)
end
function daft:EmeraldRNG(min:number, max:number) -- based off of pokemon emerald's rng, both the server and the client will disagree on cache7's value, but its still wunky...
	return daft:limit(daft.cache7, max, min)
end

return daft
15 Likes

this is the best script ever, this will help millions
@ScrollFrameRotation this is your fault

Turn this into a model
so no one can call it their own
then give examples of how it can be used here

DUDE i was just telling myself this morning “i wish i had an RNG that ran off of a fanfiction site..” and you came down from the heavens to grant me this amazing module!

Are you kidding or are you serious?

im 100 percent serious

5 Likes

this is the version i can show to the public, the other one was stained with red.

Awesome. Dont know what it does. Still awesome.

3 Likes

Anyway, it’s better to do this for ownership. You can even find out how many people are using it and have accurate statistics.

nuh uh!!!

also admittedly the only bad parts about this module script is the rand functions, even then atleast some are useful :eyes:

You are a very kind-hearted person.

how do you think it would really help? People can reupload it anyway

Really?! But, that isn’t a lot of functions, it’s some good basic utilities, but I know games need more modulescripts than just that. It can’t be the ONLY modulescript we’ll ever need, some games need so many modulescripts for different data purposes like stats, npc entries, records, etc.

You just don’t see the potential

1 Like

who even needs junk like that, all you need is daft:limit and daft:shuffle!

2 Likes

i was sick but after using this module i was cured then got cancer. thank you :folded_hands: :heart::heart::smiling_face_with_three_hearts:

1 Like

this is what daft can do to you, probably…

wuh oh there mistake here!1!!!1!!111

wuh oh!!! mods crush his skull

2 Likes