Script wont work any time!

Hello devs, I am working on a game with this script

while wait(90) do
	local Players = game.Players:GetPlayers()
	local RandomPlayer = Players[math.random(0, #Players)]
	local RigClone = game.ServerStorage.Rake:Clone()
	if #Players ~= 0 then
		RigClone.Parent = workspace
		RandomPlayer.Character:Destroy()
		RandomPlayer.Character = RigClone
		RandomPlayer.Character:MoveTo(workspace.CavePart.Position)
		for _,tool in pairs(game.ReplicatedStorage.RakeTools:GetChildren()) do
			if tool:IsA("Tool") then
				tool:Clone().Parent = RandomPlayer.Backpack
			end
		end
		game.ReplicatedStorage.ChangeCam:FireClient(RandomPlayer, RandomPlayer.Character.NPC)
	else
		warn("No players in-game")
	end
	wait(120)
	RandomPlayer:LoadCharacter()
end

And it keeps showing me this error :

ServerScriptService.Main:7: attempt to index nil with 'Character'

Can anyone please help me? Thank you!

Wut, shouldn’t it be Players[math.random(1, #Players)]? Also you should always check that there is a Character inside the Player, even if you think there is one:

while wait(90) do
	local Players = game.Players:GetPlayers()
	local RandomPlayer = Players[math.random(1, #Players)]
	local RigClone = game.ServerStorage.Rake:Clone()
	if #Players ~= 0 and RandomPlayer.Character then
		RigClone.Parent = workspace
		RandomPlayer.Character:Destroy()
		RandomPlayer.Character = RigClone
		RandomPlayer.Character:MoveTo(workspace.CavePart.Position)
		for _,tool in pairs(game.ReplicatedStorage.RakeTools:GetChildren()) do
			if tool:IsA("Tool") then
				tool:Clone().Parent = RandomPlayer.Backpack
			end
		end
		game.ReplicatedStorage.ChangeCam:FireClient(RandomPlayer, RandomPlayer.Character.NPC)
	else
		warn("No players in-game")
	end
	wait(120)
	RandomPlayer:LoadCharacter()
end

Oh i am dummyhead thank you! I didnt notice it!

You’re completely fine lol, just be sure to check if you there are valid Objects that you’re wanting to search for :sweat_smile: