Roblox Studio:尝试使用“Connect”索引 nil

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

我正在做一个脚本,输出显示“ Workspace.Script:30: attempts to index nil with 'Connect' ”。错误位于第 30 行,其中包含以下代码:

players.PlayerAdded:Connect(function(player) -- line 30

    if player.UserId == alertaCorUserId then
    alertaCorEvent:FireClient(player, true)
    else
    alertaCorEvent:FireClient(player, false)
    end

end)

我尝试了很多方法,但没有任何效果。欢迎所有帮助。我会把整个脚本放在这里

local players = game:GetService("Players"):GetPlayers()

if #players > 0 then
    
    local alertaCorUserId = players[math.random(1, #players)].UserId

    
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local alertaCorEvent = ReplicatedStorage:FindFirstChild("AlertaCorEvent")

    if not alertaCorEvent then
        alertaCorEvent = Instance.new("RemoteEvent")
        alertaCorEvent.Name = "AlertaCorEvent"
        alertaCorEvent.Parent = ReplicatedStorage
    end

    
    if alertaCorEvent then
        

        players.PlayerAdded:Connect(function(player)

            if player.UserId == alertaCorUserId then
                alertaCorEvent:FireClient(player, true)
            else
                alertaCorEvent:FireClient(player, false)
            end

        end)
        
        alertaCorEvent.OnServerEvent:Connect(function(player, isAlertaCor)
            
            if isAlertaCor then
                print(player.Name .. " is Alerta Cor!")
                
            else
                print(player.Name .. " isn't Alerta Cor!")
                
            end
        end)
        
        
        
    else
        warn("Erro: alertaCorEvent is nil.")
    end
else
    warn("No players to be selected")
end

lua roblox roblox-studio
1个回答
0
投票

game:GetService("Players")
是玩家服务,
game:GetService("Players"):GetPlayers()
是包含所有连接玩家的表。您想要将事件连接到服务,而不是该表。

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