0
votes

When trying to respawn the player with plr:LoadCharacter() it just gives me: attempt to index nil with 'LoadCharacter' & I tried multiple ways to do it such as Player:LoadCharacter() or is there a more efficient way to kill/respawn the player?

--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

1 Answers

1
votes

In this case plr is a nil value. So plr:LoadCharacter() is not allowed as it does not make any sense.

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

is the reason.

So refer to this manual page: https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer

Maybe this helps:

Loading GUIs When creating loading GUIs using ReplicatedFirst, sometimes a LocalScript can run before the LocalPlayer is available. In this case, you should yield until it becomes available by using Instance: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()