2025-07-02 00:21:32 +08:00
|
|
|
|
local Utils = {}
|
|
|
|
|
|
|
|
|
|
--> Services
|
|
|
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
|
|
|
|
|
|
|
|
--> Variables
|
2025-07-03 01:48:06 +08:00
|
|
|
|
local PlayerDataFolder = ReplicatedStorage:WaitForChild("PlayerData")
|
2025-07-02 00:21:32 +08:00
|
|
|
|
|
|
|
|
|
--> Constants
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
function Utils:GetPlayerDataFolder(Player: Player)
|
|
|
|
|
local pData = PlayerDataFolder:FindFirstChild(Player.UserId)
|
|
|
|
|
if pData then return pData end
|
|
|
|
|
warn("玩家数据不存在: " .. Player.Name)
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Utils:CreateFolder(Name: string, Parent: Instance)
|
|
|
|
|
local Folder = Instance.new("Folder")
|
|
|
|
|
Folder.Name = Name
|
|
|
|
|
Folder.Parent = Parent
|
|
|
|
|
return Folder
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Utils:SetAttributesList(Object: Instance, Attributes: table)
|
|
|
|
|
for Attribute, Value in Attributes do
|
|
|
|
|
Object:SetAttribute(Attribute, Value)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Utils:GenUniqueId(t: table)
|
|
|
|
|
local min_id = 1
|
|
|
|
|
while t[min_id] ~= nil do
|
|
|
|
|
min_id = min_id + 1
|
|
|
|
|
end
|
|
|
|
|
return min_id
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-03 01:48:06 +08:00
|
|
|
|
-- function Utils:GetJsonIdData(JsonName: string, id: number)
|
|
|
|
|
-- local JsonData = require(ReplicatedStorage.Json[JsonName])
|
|
|
|
|
-- for _, item in ipairs(JsonData) do
|
|
|
|
|
-- if item.id == id then
|
|
|
|
|
-- return item
|
|
|
|
|
-- end
|
|
|
|
|
-- end
|
|
|
|
|
-- return nil -- 没找到对应id
|
|
|
|
|
-- end
|
|
|
|
|
|
|
|
|
|
function Utils:GetIdDataFromJson(JsonData: table, id: number)
|
|
|
|
|
-- 遍历JsonData,查找id字段等于目标id的项
|
2025-07-02 00:21:32 +08:00
|
|
|
|
for _, item in ipairs(JsonData) do
|
|
|
|
|
if item.id == id then
|
|
|
|
|
return item
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-07-03 01:48:06 +08:00
|
|
|
|
return nil -- 没有找到对应id
|
2025-07-02 00:21:32 +08:00
|
|
|
|
end
|
|
|
|
|
|
2025-07-03 01:48:06 +08:00
|
|
|
|
function Utils:GetMaxIdFromJson(JsonData: table)
|
|
|
|
|
local maxId = 0
|
2025-07-02 00:21:32 +08:00
|
|
|
|
for _, item in ipairs(JsonData) do
|
2025-07-03 01:48:06 +08:00
|
|
|
|
if item.id > maxId then
|
|
|
|
|
maxId = item.id
|
2025-07-02 00:21:32 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2025-07-03 01:48:06 +08:00
|
|
|
|
return maxId
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 将字符串数组转化成真正的数组表,"[1,10,2,10]" -> {1,10,2,10}
|
|
|
|
|
function Utils:StringArrayToTable(StringArray: string)
|
|
|
|
|
if type(StringArray) ~= "string" then return {} end
|
|
|
|
|
-- 去除首尾的中括号
|
|
|
|
|
local content = StringArray:match("%[(.*)%]")
|
|
|
|
|
if not content or content == "" then return {} end
|
|
|
|
|
local result = {}
|
|
|
|
|
for value in string.gmatch(content, "[^,]+") do
|
|
|
|
|
local num = tonumber(value)
|
|
|
|
|
if num then
|
|
|
|
|
table.insert(result, num)
|
|
|
|
|
else
|
|
|
|
|
table.insert(result, value)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return result
|
2025-07-02 00:21:32 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
return Utils
|