尝试使用“LoadCharacter”索引 nil

时间:2021-05-12 13:10:40

标签: lua roblox

当尝试使用 plr:LoadCharacter() 重生玩家时,它只会给我: 尝试使用“LoadCharacter”索引 nil 并且我尝试了多种方法来做到这一点,例如 Player:LoadCharacter() 或者是否有更有效的方法来杀死/重生玩家?

--Declared Boolean Global variable
_G.TimerStart = false

--Local Paths to Objeccts
local label = game.StarterGui.TimerGUI.Timer

-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer

--Get Children
local Child = game.Players:GetChildren()

-- Wait for Child Variables
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
local timerevent = ReplicatedStorage:WaitForChild("Timer")

local function Thieves(Players)
    if _G.TimerStart == false then
        for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
            game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
            wait(2)
            plr:LoadCharacter()
            wait(7)
            timerAmount = 120
        end
        for i,v in pairs(game.Teams.Police:GetPlayers()) do
            game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
            wait(2)
            plr:LoadCharacter()
            wait(7)
            timerAmount = 120
        end
    end 
end

1 个答案:

答案 0 :(得分:1)

在这种情况下,plr 是一个 nil 值。所以 plr:LoadCharacter() 是不允许的,因为它没有任何意义。

local plr = game:GetService("Players").LocalPlayer

是原因。

因此请参阅此手册页:https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer

也许这有帮助:

<块引用>

加载 GUI 使用 ReplicatedFirst 创建加载 GUI 时,有时 LocalScript 可以在 LocalPlayer 可用之前运行。 在这种情况下,您应该使用 yield 直到它变得可用 实例:GetPropertyChangedSignal

local Players = game:GetService("Players")
-- Below: access Players.LocalPlayer; if it is nil, we'll wait for it using GetPropertyChangedSignal.
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()