【问题标题】:Roblox In-Game Ban SystemRoblox 游戏内禁令系统
【发布时间】:2020-12-17 23:52:45
【问题描述】:

我正在尝试制作一个游戏内系统来禁止玩家。我有一个按钮,可以触发带有玩家姓名的远程事件以及为什么他们被禁止的消息。但是每次我按下按钮时,我都会收到这个错误:

ServerScriptService.Event_Handler:21: attempt to call a nil value

我不知道为什么这不起作用,有人可以帮助我了解发生了什么问题吗?

EVENT_HANDLER

local dss = game:GetService("DataStoreService")
local bands = dss:GetDataStore("banDataStore")

BanPlayer.OnServerEvent:Connect(function(player, playertoban, reason)
    local pui = player.UserId
    local success, errormessage = pcall(function()
        bands:SetAsync("Banned-", pui, true)
    end)
    
    if success then
        print("Player Successfuly Banned")
    end
    game.Players:FindFirstChild(playertoban):Kick(reason)
end)

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    由于您是按名称搜索玩家,因此您可能拼错了名称。在这种情况下,game.Players:FindFirstChild() 将返回 nil。你可以通过在调用Kick()之前确保玩家存在来清理这个调用

    另外,作为旁注,您似乎正在禁止调用BanPlayer RemoteEvent 的玩家,而不是名称存储在playertoban 中的玩家。

    local dss = game:GetService("DataStoreService")
    local bands = dss:GetDataStore("banDataStore")
    
    BanPlayer.OnServerEvent:Connect(function(player, playertoban, reason)
        -- check that playertoban is a real player's name
        local bannedPlayer = game.Players:FindFirstChild(playertoban)
        if not bannedPlayer then
            warn("Could not find a player named " .. playertoban)
            return
        end
    
        -- record their user-id so we can ban them when they rejoin
        local pui = bannedPlayer.UserId
        local success, errormessage = pcall(function()
            bands:SetAsync("Banned-", pui, true)
        end)
        if success then
            print(playertoban .. " Successfully Banned")
        else
            warn(string.format("Failed to ban %s permanently with error : %s", playertoban, errormessage))
        end
    
        -- remove them from the game
        bannedPlayer:Kick(reason)
    end)
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2020-07-17
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多