我目前正在尝试开发 Roblox 游戏;有人可以帮助我找到模块吗?

问题描述 投票:0回答:1

这是一个模块脚本,它在ServerScriptService中:

local PlayerDataHandler = {}

local dataTemplate = {
    Race = "",
    Cash = 0,
    Inventory = {},
    Level = 0,
}

local ProfileService = require(game.ServerScriptService.ProfileService)
local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore("PlayerProfile", dataTemplate)

local Profiles = {}

local function playerAdded(player)
    repeat wait() until player:GetAttribute("slot") ~= nil

    local slot = player:GetAttribute("slot")
    print("Loaded with slot", slot)

    local profile = ProfileStore:LoadProfileAsync(slot .. player.UserId)

    if profile then
        if profile.Error then
            warn("Failed to load profile for player", player.UserId, "- Error:", profile.Error)
            player:Kick()
            return
        end

        profile:Reconcile()

        profile:ListenToRelease(function()
            Profiles[player] = nil
            if player:IsDescendantOf(Players) then
                print("Profile released for player", player.UserId, "- Kicking player.")
                player:Kick()
            end
        end)

        if not player:IsDescendantOf(Players) then
            profile:Release()
        else
            Profiles[player] = profile
            print("Loaded profile for player", player.UserId)
            print(Profiles[player].Data)
        end
    else
        print("Failed to load profile for player", player.UserId)
        player:Kick()
    end
end

function PlayerDataHandler:Init()
    for _, player in ipairs(game.Players:GetPlayers()) do
        task.spawn(playerAdded, player)
    end

    game.Players.PlayerAdded:Connect(playerAdded)

    game.Players.PlayerRemoving:Connect(function(player)
        if Profiles[player] then
            Profiles[player]:Release()
        end
    end)
end

local function getProfile(player)
    assert(Profiles[player], string.format("Profile does not exist for %s", player.UserId))

    return Profiles[player]
end

function PlayerDataHandler:Get(player, key)
    local profile = getProfile(player)
    assert(profile.Data[key], string.format("Data does not exist for key: %s", key))

    return profile.Data[key]
end

function PlayerDataHandler:Set(player, key, value)
    local profile = getProfile(player)
    assert(profile.Data[key], string.format("Data does not exist for key: %s", key))

    assert(type(profile.Data[key]) == type(value))

    profile.Data[key] = value
end

function PlayerDataHandler:Update(player, key, callback)
    local profile = getProfile(player)

    local oldData = self:Get(player, key)
    local newData = callback(oldData)

    self:Set(player, key, newData)
end

return PlayerDataHandler

此脚本调用模块模块脚本,没有出现错误:

local PlayerDataHandler = require(game.ServerScriptService:WaitForChild("PlayerDataHandler"))

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    
    if player then 
        PlayerDataHandler:Update(player, "Cash", function(currentCash)
            return currentCash + 300
        end)
        
        PlayerDataHandler:Update(player, "Inventory", function(currentInventory)
            table.insert(currentInventory, "Bag")
            
            return currentInventory
        end)
        
        script.Parent:Destroy()
        
        print(PlayerDataHandler:Get(player, "Cash"))
        print(PlayerDataHandler:Get(player, "Inventory"))
    end
end)

这个脚本是我遇到问题的脚本,它位于 PlayerGUI 中,当我尝试调用它时无法找到模块脚本,从而导致此错误,“StarterPlayer.StarterCharacterScripts:WaitForChild("PlayerDataHandler")”上可能出现无限产量:

local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(
    .1,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.Out
)

local tween = TweenService:Create(script.Parent.Parent.Slot2, tweenInfo, {Position = UDim2.new(0.661, 0, 0.126, 0)})
local tween2 = TweenService:Create(script.Parent.Parent.Slot2, tweenInfo, {Position = UDim2.new(0.681, 0, 0.126, 0)})

local tween3 = TweenService:Create(script.Parent.Parent.Slot3, tweenInfo, {Position = UDim2.new(0.122, 0, 0.126, 0)})
local tween4 = TweenService:Create(script.Parent.Parent.Slot3, tweenInfo, {Position = UDim2.new(0.102, 0, 0.126, 0)})

local right = script.Parent[">"]
local left = script.Parent["<"]

local tween5 = TweenService:Create(left, tweenInfo, {Position = UDim2.new(1.162, 0, 0.442, 0)})
local tween6 = TweenService:Create(left, tweenInfo, {Position = UDim2.new(1.062, 0, 0.442, 0)})
local tween7 = TweenService:Create(right, tweenInfo, {Position = UDim2.new(-0.292, 0, 0.442, 0)})
local tween8 = TweenService:Create(right, tweenInfo, {Position = UDim2.new(-0.192, 0, 0.442, 0)})

local isMouseOver = false
local isButtonClicking = false

script.Parent.MouseEnter:Connect(function()
    if not isMouseOver then
        isMouseOver = true

        local n = 157
        local n2 = 180

        right.TextTransparency = 0
        left.TextTransparency = 0
        script.Parent.Sound:Play()
        tween2:Play()
        tween4:Play()
        tween6:Play()
        tween8:Play()
        repeat
            right.TextTransparency -= 0.1
            left.TextTransparency -= 0.1
            task.wait(0.01)
        until right.TextTransparency <= 0 and left.TextTransparency <= 0 or not isMouseOver
    end
end)

script.Parent.MouseLeave:Connect(function()
    if isMouseOver then
        isMouseOver = false

        right.TextTransparency = 1
        left.TextTransparency = 1
        tween:Play()
        tween3:Play()
        tween5:Play()
        tween7:Play()
    end
end)

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Biwa:Play()
    if not isButtonClicking then
        isButtonClicking = true

        -- Fade out the current slots and title
        repeat
            script.Parent.Parent.SlotsTitle.TextTransparency += 0.2
            script.Parent.Parent.Slot1.TextTransparency += 0.2
            script.Parent.Parent.Slot2.TextTransparency += 0.2
            script.Parent.Parent.Slot3.TextTransparency += 0.2
            script.Parent.Parent.Slot1.BackgroundTransparency += 0.2
            script.Parent.Parent.Slot2.BackgroundTransparency += 0.2
            script.Parent.Parent.Slot3.BackgroundTransparency += 0.2
            script.Parent.Parent.Back.TextTransparency += 0.2
            task.wait(0.01)
        until script.Parent.Parent.SlotsTitle.TextTransparency >= 1

        -- Hide current UI elements
        script.Parent.Parent.Visible = false

        -- Load PlayerDataHandler module
        local PlayerDataHandler = require(game.StarterPlayer.StarterCharacterScripts:WaitForChild("PlayerDataHandler"))

        local player = game.Players.LocalPlayer
        local race = PlayerDataHandler:Get(player, "Race")

        if race ~= "" then
            print("Selection")
        else
            print("Not seleted")
        end
    end
end)

lua server-side client-side roblox roblox-studio
1个回答
0
投票

出于安全原因,LocalScripts 无法访问 ServerStorage 或 ServerScriptService。

只需将 ModuleScript 移动到 ReplicatedStorage,然后将您的

require
路径更新到新位置。

© www.soinside.com 2019 - 2024. All rights reserved.